# Pub/Sub

## General

The publish-subscribe pattern (or pub/sub, for short) is a messaging pattern where senders of messages (publishers), do not program the messages to be sent directly to specific receivers (subscribers). Instead, the programmer “publishes” messages (events), without any knowledge of any subscribers there may be.

Similarly, subscribers express interest in one or more events and only receive messages that are of interest, without any knowledge of any publishers.

![Pub/Sub Message Pattern](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWo9-utUu1Q8v2C%2Fpubsub.png?alt=media\&token=e003b332-df67-4ffb-be81-6b9218cefc4e)

## Core Features

KubeMQ supports Publish-Subscribe messages patterns with the following core features:

* **Events** - An asynchronous real-time Pub/Sub pattern.
* **Events Store** - An asynchronous Pub/Sub pattern with persistence.
* **Grouping** - Load balancing of events between subscribers
* **Partitioning** - Channels/Topics based wildcards

## Events

Events are an asynchronous real-time Pub/Sub pattern. In Events, multiple senders can send real-time messages to various receivers; however, only if they are currently connected to KubeMQ; there is no message persistence available in this pattern.

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWrFAWO0nJaML_Q%2Fevent.png?alt=media\&token=1c03d30a-233d-4426-b016-2e792ecaa03b)

**Use Cases**

‘Events’ pattern is suitable for cases such as publishing streaming data, logs, notifications, etc.

### Demo - Basic

{% embed url="<https://vimeo.com/372195988>" %}

### Demo - Group (Load Balancing)

{% embed url="<https://vimeo.com/372195963>" %}

### Demo - Wildcards

{% embed url="<https://vimeo.com/372196013>" %}

## Events Store

Events Store is an asynchronous Pub/Sub pattern with persistence. In Events Store, multiple senders can send messages to various receivers even if they are not currently. Any receiver can connect to KubeMQ and replay one, any, or all of the messages stored for a specific channel.

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWswCS8m9TBaucD%2Fevent-store.png?alt=media\&token=8d5216e9-c5e5-4fc2-bb3c-509b252884d9)

### Events Store Replay Messages Types

KubeMQ supports six types of Events Store subscriptions and replay:

| Type            | Description                                                                                                      |
| --------------- | ---------------------------------------------------------------------------------------------------------------- |
| New Events      | KubeMQ will send only new events                                                                                 |
| First Event     | KubeMQ will replay all events from the first stored events, as well as send new events                           |
| Last Event      | KubeMQ will replay the last event and continue to send new events                                                |
| From Sequence   | KubeMQ will replay events from a specific sequence and continue to send new events                               |
| From Time       | KubeMQ will replay events from a specific time in the past and continue to send new events                       |
| From Time Delta | KubeMQ will replay events from the particular time delta back (i.e., 5 min back) and continue to send new events |

#### Start From New Events

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWwmh-lb2Y1GpPf%2Fevent-store-from-new.png?alt=media\&token=70d59306-158e-4f4f-ba4e-807993987b0c)

{% embed url="<https://vimeo.com/372195866>" %}

#### Start From First Event

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWx3ifbjV9dP5jA%2Fevent-store-from-first.png?alt=media\&token=394d43e3-a46d-49b6-8c3f-89643e50a5bc)

{% embed url="<https://vimeo.com/372196147>" %}

#### Start From Last Event

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWyXOmPqZDwwKyw%2Fevent-store-from-last.png?alt=media\&token=42e23180-40f2-4d77-a2be-c677ef7c3a3b)

{% embed url="<https://vimeo.com/372196161>" %}

#### Start From Sequence

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWv0X_3Xa5Ggd3V%2Fevent-store-from-seq.png?alt=media\&token=9c4eba6a-e5f6-40c6-affb-05b8d3ef830d)

{% embed url="<https://vimeo.com/372195881>" %}

#### Start From Time

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWtYW6jP-XbXr5C%2Fevent-store-from-time.png?alt=media\&token=93d7da91-0cb9-4aaa-8d36-2e890d629e59)

{% embed url="<https://vimeo.com/372195889>" %}

#### Start From Time Delta

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWu6CRygkNLpUg_%2Fevent-store-from-time-delta.png?alt=media\&token=fbeb9f3f-25dd-4800-a477-570940048512)

{% embed url="<https://vimeo.com/372195899>" %}

### Grouping - Load Balancing

KubeMQ supports grouping (load balancing) of multiple receivers to share the load

#### Demo

**Example 1**

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chOTT5f2vWhb_wrYc%2Fkubemqctl-pub-sub-events-store-groups-1.gif?alt=media\&token=3a4d78b8-0317-411d-b70b-1322f364827d)

**Example 2**

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chOTSHnBEkkgA-01G%2Fkubemqctl-pub-sub-events-store-groups-2.gif?alt=media\&token=4970cfb9-4379-4d30-81f6-eac2632e9f97)

**Example 3**

![](https://2828835384-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chOTR8glwyBhRLmfn%2Fkubemqctl-pub-sub-events-store-groups-3.gif?alt=media\&token=4b035cc9-98a7-40e9-94ca-9e05c111b0f6)

### Unique Client ID

The uniqueness of a client ID is essential when using Events Store. At any given time, only one receiver can connect with a unique Client ID. If two receivers try to connect to KubeMQ with the same Client ID, one of them will be rejected.

**Client ID and Subscription Types Relations**

For each unique Client ID, KubeMQ saves the subscription type in which the client connected; messages can only be replayed once per Client ID and Subscription type.

For example, Receiver with Client ID `client-foo-1` subscribes to a channel `foo.bar` in `First Event` mode. They will get all the messages stored in KubeMQ for `foo.bar` channel from the first message, and then continue to get new events as they come. If this receiver will disconnect from KubeMQ and re-connect again with any subscription type, only new events in `foo.bar` will be delivered for this specific receiver with Client ID `client-foo-1`.

If a Receiver wishes to receive messages on `foo.bar` again, they should subscribe again with a different Client ID than `client-foo-1` such `client-foo-1-retry`.

**Use Cases**

Events Store pattern is suitable for cases in which events are necessary, such as worker’s pool, chats, and inbox related applications.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kubemq.io/learn/message-patterns/pubsub.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
