How to create a blazing Pusher chat with Delphi ?

Pusher is a set of bidirectional APIs targeted on real time communication. The main use case of those APIs are :

  • real time charts (cryptocurrency values)
  • network games (synchronization of players)
  • in app chat (application support)
  • real time resuts (basketball, soccer, baseball)
  • instant geolocalization (products to home delivery)

Pusher is a publish-subscribe messaging system that allows to exchange messages reliably and effectively. Pusher solves by design the problems of :

  • scale of number of messages and users
  • the real time constraint of messages delivery.

Pusher distings itself from other publish-subscribe systemby introducing the concept of type of channels.

A channel of communication can have 4 differents type :

  • Public
  • Private
  • Encrypted
  • Presence

Public Channel

Public channels should be used for publicly accessible data as they do not require any form of authorisation in order to be subscribed to.

Private Channel

Private channels should be used when access to the channel needs to be restricted in some way. In order for a user to subscribe to a private channel permission must be authorized. The authentication occurs via a HTTP Request to a configurable authentication url when the subscribe method is called with a private- channel name.

Encrypted Channel

Encrypted channel is similar to a private channel. However the content of message is end-to-end encrypted  This encryption follows the specifications of Secretbox encryption standard defined in NaCl and message is encrypted before it leaves the server. Only authorized subscribers have access to the channel specific decryption key. This means that nobody except authorized subscribers can read the payload data, not even Pusher. End-to-end encrypted channels also provide end-to-end authenticity; that is your messages are protected from forgery, and that they are guaranteed to come from someone who holds the encryption master key.

Presence Channel

Presence channels build on the security of Private channels and expose the additional feature of an awareness of who is subscribed to that channel. This makes it extremely easy to build chat room and “who’s online” type functionality to your application.

Create application on Pusher.com

First of all as for facebook or telegram, user needs to subscribe to Pusher network and creates an application. Pusher will generate several id and keys needed to used Pusher system as follow :

untitled

With all these data, the developer will be able to interact with Pusher system.

Connect to Pusher network with the TsgcWSAPI_Pusher component of ESEGECE

As the low-level communication with Pusher is based on web socket, it’s quiet obvious to find a Pusher component in the websocket component suite of ESEGECE company.

Download  the component suite from here. The component installation is done by the compilation of the bpl regarding the Delphi version.

Create a new application and drop on the main form the web socket component TsgcWebSocketClient and the pusher component TsgcWSAPI_Pusher.

In the property of the pusher component we will need to set :

for the websocket component the property will be set a following :

The cluster value ‘eu’ is used as a subdomain connection. The use of a secure conection (TLS) raises a dependancy with two dll of openssl : libeay32.dll and ssleay32.dll (for Win32). Do not forget to add these libraries in the same directory of the delphi application or in a directory defined in the PATH system variable.

When both component are parametrized we can associate them by setting that the pusher client is the websocket component :

To connect to the Pusher network just activated the websocket component :

In case of the application is connected an event OnConnect is fired. If an error occured an event OnError or OnPusherError is raised depending of the error.

How to subscribe to a Pusher channel ?

Overview

User can subscribe to a Pusher channel by calling the method  :

When the subscription is successfull an event OnPusherSubscribe is raised

We can notice that by default the subscription is done for public channel.

Naming convention

According to Pusher documentation, a public channel has no naming rule but private channel must start with ‘private-‘ and presence channel ‘presence-‘. Actually this naming convention is done by setting the channel type during the call of the Subscribe method.

Exemple :

will subscribe the channel ‘private-privatename’. The concatenation of the name used as a variable in the Subscribe call with ‘private-‘ as a suffix is done by the TsgcWSAPI_Pusher class. The same behaviour is implemenented for presence channel.

The particular case of subscription of presence channel

When user subscribes to a presence channel, the system waits for information regarding the user. Then the data variable of the subscribe method call must be set as following :

The user_id field value must be of course unique. Use an unique identifier as an email or GUID for this field for exemple.

The user data as json will be published immediately during subscription to all subscribers of this presence channel.

How to publish a message in a Pusher channel ?

The user can use the method Publish as follow for any channel type :

Then when the user will call Publish, if another Pusher client subscribed the same channel for the same application, it will receive an event OnPusherEvent(Sender: TObject; Event, Channel,
Data: string)
containing all message parameters.

For public channels, the user can use the TriggerEvent method. It’s a transcription of the Pusher API triggerevent limited to public channel.

Then when the user will call TriggerEvent, if another Pusher client subscribed the same public channel for the same application, it will receive an event OnPusherEvent.

How to get all available channels ?

When the client is connected to the Pusher cloud application, it is possible to know all subscribed channels by calling the method GetChannels.

It returns synchronously  a json string will all available channel names.

Please find a whole demo at https://github.com/ThierryGrassia/PusherDemo . Two VCL projects can be found. One with the whole Pusher feature implemented and another one where the secret key was not shared and this application can only listen messages on public channels.

 

 

 

 

 

 

 

 

 

 


What's New for RAD Studio 11