Kuzzle blog

Pub/Sub: key concepts and its usage for real-time application

Written by Kuzzle Team | May 03, 2019

Version française

 

 Web and mobile applications are more and more expected to update their information in real time

 

Whether to follow stock market prices, book a cab or chat on instant messaging, the application must be updated without requiring any action from the user.

 

In this article, we will see what are the concepts behind real-time applications with Pub/Sub and then we will see how to use it with Kuzzle.

 

This article is part of a series on real-time application development:

 

 

Pub/Sub: notify your clients in real time

 

The concept of Publisher/Subscriber, or Pub/Sub, is a concept in which a communication channel (topic) is set up between a server and several clients. This channel is represented by a persistent network communication (WebSocket, MQTT, etc.).

 

Customers, or subscribers, will first subscribe to notifications received in a specific channel. Then the server, or publisher, will send data to the clients through this channel.

 

 

 

 

So when new information is available, the server can notify the clients.

 

Make your Pub/Sub development easier with Kuzzle

 

 

With Kuzzle, it is very easy to implement the Pub/Sub concept in your application.

 

For this we will use:

 

 

First of all, we will subscribe to a notification channel. In Kuzzle, a channel is identified by the name of an index, the name of a collection and filters.

 

We will come back later on the filters, concerning the index and the collection, it is not necessary that these really exist in base for the Pub/Sub, they only serve to define a communication channel.

 

To subscribe to channel notifications, we will use the Realtime controller's subscribe method. The last parameter is a callback that will be called for each new notification received.

 

const filters = {};

await kuzzle.realtime.subscribe('index', 'collection', filters, notification => {
  // Do something with the notification
});

 

We will now publish messages in the same channel, they will be transmitted to all customers subscribing to this channel.

For this we will use the publish method of the Realtime controller.

 

await kuzzle.realtime.publish('index', 'collection', { content: 'liia meh ry' });

 

Customers subscribing to this communication channel will all receive the message { content: ‘liia meh ry’ }.

 

The implementation of the Pub/Sub concept in an application is very simple with the help of Kuzzle and our SDKs.

 

The use of a persistent protocol is necessary to be able to do Pub/Sub, for your web or mobile applications we recommend to use the WebSocket protocol and for your IoT applications you can use the MQTT protocol.

 

Complexity of Pub/Sub development

 

Although simple to implement, the Pub/Sub concept can pose problems of maintainability and scalability.

 

For each piece of information you want to make available in real time, you must create a new communication channel, your customers must subscribe to it and then finally you must send your data to it.

 

It is necessary to have a certain amount of server side code to develop, test and maintain

 

In Kuzzle, even if we really like real time, we find that the Pub/Sub concept is limited and that its implementation requires many resources.

 

We have therefore developed our own real-time engine to overcome these problems and accelerate the development of real-time applications.

 

See you next week for the rest of this series of articles on real-time applications!