Pub/Sub

Pub/Sub provides a type of flexible mechanism to tackle varieties of applicaton requirements.

This document introduces to you its related concepts and considerations, and please refer to the corresponding integration guide and API documentation when using it.

Channel

Channel is the transport channel for Event.

If the client is to receive an Event, it must subscribe a particular Channel and bind the handler of the Event.

If the sender is to send an Event, it must trigger on the same Channel so that the handler bound on the client will be called.

Overviw

Channel mainly plays two roles:

  • Filter Event: For example, if there is a Channel used exclusively to discuss cats in a chat applicaiton, then those who dislike cats may not subscribe this Channel.
  • Control access permissions of Event: For instance, you can control that a particular Channel is only available for the administrator to subscribe.

Each application can have multiple Channels, and various clients can opt to subscribe which Channel.

Channel is created on demand, and no need to show declaration in advance.

After the client has subsribed a specific Channel, even though the handler is not bound to it, Event will still be transferred to the client from network.

Naming Conventions

A max of 128 characters is allowed, and can only contain uppercase and lowercase letters, numbers as well as these punctuations: _ - = @ , . ;.

Subscribe

Usually the handler is bound by calling the subscribe method on the Engine object of the client SDK.

For more details, see the corresponding SDK documentation.

Unsubscribe

Usually the handler is bound by calling unsubscribe on the Engine object of the client SDK.

For more detials, see the corresponding SDK documentation.

Category

Currently, there are three categories:

  • Public Channel: The default category, and no permission control. Anyone who knows the name can subscribe it.
  • Private Channel: The name must contain private- as its prefix, and mechanism user authentication is provided.
  • Presence Channel: The name must contain presence- as its prefix, and the service of online user status is provided based on “Presence Channel”.

Special Event

All the Channels have these special Events:

  • engine:subscription_succeeded: Triggered when subscribed successfully.
  • engine:subscription_error: Triggered when subscription failed, which is usually due to illegal subscription request, for example, user authentication failure.

Private Channel

The name must contain private- as its prefix.

When a client attempts to subscribe this type of Channel, the client SDK will automatically initiate an HTTP request to the address you have configured to authenticate the user. Your server that receives the request is supposed to handle and return the result as required. For more details, see Authenticate User. And for the details on how to configure the address, please refer to the corresponding SDK documentation.

For more details, see the corresponding SDK documentation.

Presence Channel

The name must contain presence- as its prefix.

The mechanism of user authentication is the same as that of Private Channel, but the difference is that the authentication callback response can return additional user data, which will be stored in Realtime Engine and then be accessed through related API. For more details, see Authenticate User.

Presence Channel has following limitations:

  • A max of 1000 users is allowed for each Channel.
  • A max of 128 characters is allowed for the userId.
  • A max of 1KB is allowed for the userInfo.

User Acqusition List

You can get the detailed information of all the users on a particular Presence Channel:

  • On the client: Through SDK interface. For more details, please refer to the corresponding documentation.
  • On the server: By calling Realtime Engine Open API.

Special Event

Presence Channel has the following special Events in addition:

  • engine:subscription_succeeded: Will Offer another parameter of the user list in addition.
  • engine:user_added: Triggered when new users subscribe the Channel. If multiple Connections are being used simultaneously by the same user, for example, multiple tabs are open in a browser, only the first will trigger the Event.
  • engine:user_removed: Triggered when users leave the Channel, which is probably because the client unsubscribes actively, network gets disconnected or the same user actively terminates your application. If multiple Connections are being used the user, for example, multiple tabs are open in a browser, only when all of them are closed can the Event be triggered.

For more details, please refer to the corresponding SDK documentation.

Event

Event, i.e., an event, is the basic unit of Pub/Sub communication. When one party wants to send messages to the other, one Event needs to be triggered.

Usually Event is triggered by the server, but the client can also trigger it under additional limitations.

Note that in the realtime engine the Event also generally refers to any particular event that is triggered by any particular action, for example, users go online and offline in the Chat mode. Please distinguish from the contex while reading related documentation.

Overview

Event is the message packaging in the Pub/Sub mode, and all communication is based on Event.

They are essentially a type of named message, and you can bind handler on the client to process the messages with particular names. You can also understand it as message routing on client. When messages reach the client, they are routed to different handlers according to their names, so rather than use it to filter messages, you should make use of the more efficient Channel to filter on the server of the Realtime Engine.

Composition

An Event consists of two parts:

  • name: any string, and usually in past tense, such as comment-added and price-changed. A max of 128 characters is allowed.
  • data: String or Object, a max of 10KB is allowed.

Bind Handler

Usually the handler is bound by calling the bind method on the Channel object of the client SDK.

For more details, please refer to the corresponding SDK documentation.

Unbind Handler

Typically the handler is unbound by calling the unbind method on the Channel object of the client SDK.

For more details, please refer to the corresponding SDK documentation.

Category

Events can be categorized as follows according to the trigger and function:

  • Connection Event: Triggered by Realtime Engine to notify Connection of the status change. For more details, see Connection.
  • Channel Event: Triggered by “Realtime Engine” to notify Channel of the subscription result. For more detais, see Channel.
  • Server Event: The most commonly used Event, which is triggered by using the SDK or directly calling the Open API on your server. And Secret needs to be provided for calling the open API. For more details, please refer to the corresponding documentation, or directly invoke Realtime Engine Open API.
  • Client Event: Triggered by your client. For more details, see below.

Client Event

Please stay tuned.

  Back To Top