> Site index: https://usehardal.com/llms.txt
> Every content route also serves markdown at <url>.md

# What is server-side Pub/Sub Messaging?

Pub/Sub model is a messaging solution for distributed systems, optimizing server-side and first-party analytics data transfer.

Source: https://usehardal.com/server-side-pub-sub
Published: 2024-09-16
Updated: 2024-09-16
Author: Berkay Demirbas

---

**Pub/Sub (Publish/Subscribe)** is a messaging model used to facilitate data transmission and communication, especially in distributed systems. Essentially, the model has two key roles: **publisher** and **subscriber**.

With Hardal, you can operate this system in a fully **server-side** and **first-party** mode.

![Hardal pub-sub](https://res.cloudinary.com/hardal/image/upload/v1726577931/rbx0yevn7pkl7qfe7dmq.svg)

### How Does Pub/Sub Work?

- **Publisher**: Publishers produce messages and publish them to specific **topics**. Publishers do not know who receives the message.
    
- **Subscriber**: Subscribers subscribe to the topics they are interested in. They receive messages only from the topics they are subscribed to. A subscriber can subscribe to one or more topics.
    
- **Message Broker**: A message broker mediates between publishers and subscribers. Publishers send messages to brokers, and brokers deliver these messages to the appropriate subscribers. *- Think of this as Hardal in this case.*

## Why Do Companies Need Pub/Sub?

You can direct event-based analytics data from end-user applications or server data from your system (this could be your company's CRM or another first-party data source) to Pub/Sub. You can later process this data with stream processing tools such as DataFlow and use it in databases.

Examples of these databases include **BigQuery** and **Cloud Storage**.

In summary, Pub/Sub allows you to collect events from multiple clients simultaneously.

Thus, using the **Pub/Sub** model server-side can provide the following advantages:

### 1. **Real-Time Data Processing**

With Pub/Sub, user actions on the site (adding items to cart, making purchases, product reviews, etc.) can be collected in real-time. This enables instant opportunities such as notifications and marketing campaigns. For example:

- **Instant Campaigns**: If many users add a particular item to their cart, a short-term discount on that product can be launched.
- **Personalized Recommendations**: You can offer better product suggestions to customers using real-time data.

### 2. **Scalability**

E-commerce sites often experience high traffic, especially during sales or special events. Pub/Sub can manage events from thousands or even millions of users simultaneously, ensuring your system is not overloaded. This allows:

- **Stability Under High Traffic**: During busy periods, data transfer continues smoothly without degrading system performance.
- **Expandable System**: As your customer base grows, you can easily scale your infrastructure.

### 3. **Asynchronous Operations**

Operations such as order processing, payment, or product inquiries can be handled asynchronously, reducing wait times for customers and enabling background tasks to continue smoothly:

- **Fast User Experience**: Customers don’t need to wait while their order is processed in the background.
- **Smooth Processes**: Tasks like order processing, invoicing, or stock updates can occur in sequence and without disruption.

### 4. **Preventing Data Loss**

With Pub/Sub, incoming data is queued and saved before being processed, preventing data loss in the event of server outages or errors. This is crucial for critical e-commerce events such as purchases or payment information:

- **Data Security**: Users’ purchase history, payment details, or product orders are not lost.
- **Fault Tolerance**: Even if your server goes down temporarily, the system resumes processing the data where it left off once it recovers.

### 5. **Segmentation and Targeting**

By analyzing the user interaction data collected via Pub/Sub, you can create customer segments, enabling more targeted marketing campaigns and personalized experiences:

- **Personalized Emails**: For example, you can send reminder emails to users who left items in their cart.
- **Dynamic Ads**: Create product campaigns and ads tailored to different customer segments.

### How to Use?

You can use the Hardal Custom Endpoint API with two request methods.

## POST
`/pubsub`

### Request Format

| Parameter    | Type           | Required | Description                               |
|--------------|----------------|----------|-------------------------------------------|
| **messages** | `Message[]`     | Yes      | List of messages to be sent               |
| **pubsub**   | `"gcp"`         | Yes      | Pub/Sub cloud provider, such as AWS, GCP, etc. |
| Extra Parameters| `string` | No       | Additional query parameters are treated as attributes |

### Message Type

| Field         | Type               | Description                                 |
|---------------|--------------------|---------------------------------------------|
| **value**     | `string`           | The content of the message                  |
| **attributes**| `{"key": "value"}` | Additional attributes related to the message|
| **messageId** | `string`           | The unique ID of the message                |
| **publishTime**| `DateTime String` | The time the message was sent (ISO 8601)    |

### Example POST Request

```bash
curl -X POST https://s2s.yourcustomdomain.com/pubsub \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR-HARDAL-SUPER-SECRET-TOKEN" \
  -d '{    
    "messages": [
      {
        "value": "Test",
        "attributes": {
          "language": "English",
          "greeting": "formal"
        },
        "messageId": "c1d7e88a-4e35-4e8e-9a0f-8c9fbd9d1123",
        "publishTime": "2024-09-09T12:30:45.123Z"
      }
    ],
    "pubsub": "gcp"
  }'
```

## GET
`/pubsub`

### Query Parameters

| Parameter       | Type     | Required | Description                                      |
|-----------------|----------|----------|--------------------------------------------------|
| **CLICK_ID**    | `string` | Yes      | The click ID                                     |
| **PUBSUB**      | `"gcp"`  | Yes      | Pub/Sub cloud provider, such as AWS, GCP, etc.   |
| Extra Parameters| `string` | No       | Additional query parameters are treated as attributes |

### Example GET Request

```bash
curl "https://s2s.yourcustomdomain.com/pubsub?CLICK_ID=112233&PUBSUB=gcp&param1=value1" \
  -H "Authorization: Bearer YOUR-HARDAL-SUPER-SECRET-TOKEN"
```

## Sources
- [AWS Pub/Sub](https://aws.amazon.com/what-is/pub-sub-messaging/)
- [Google Cloud Pub/Sub](https://cloud.google.com/pubsub/docs/overview)
