PubNub provides strong capabilities to write Client-Server applications using its highly flexible and scalable Publish and Subscribe implementation.
This article describes a basic client/Server implementation using the PubNub C# API. We will see how a Server can push messages into a channel and how Clients stay tuned to receive messages from Server. This is a completely .NET solution presenting the messaging techniques of PubNub to build a Client/Server model. We will first look at what is a Server in our experiment and go through the steps to build it. After that we will see how to build a client.
The role of the server here is very simple. It will publish messages into a specific channel. Lets call it a Messaging Server. You can see the Messaging Server in the image below.
There are two inputs, Channel Name & Content to broadcast. There is a button at the bottom, "BROADCAST" which will publish the message into the specified channel using PubNub C# API.
Publishing the message using PubNub API is very simple. OnClick() of "BROADCAST" button is defined as in the code snippet below.
private void BroadCastOnClick()
{
pubnub = new Pubnub(PUBLISH_KEY, SUBSCRIBE_KEY, SECRET_KEY, false);
string channel = txtChannel.Text;
List<object> info = pubnub.publish(channel, richTextBox1.Text);
ShowStatus(info);
}
Publishing messages involves 2 steps:
Step 1. Instantiate the PubNub API. You need to have 3 keys namely - PUBLISH_KEY, SUBSCRIBE_KEY & SECRET_KEY to instantiate PubNub API as in the above code snippet.Step 2. Make a call to the "Publish(<Channel>, <Message>)" method on PubNub API. You need to pass the 'Channel Name' and the 'Message to publish' as parameters to this method as shown in the above snippet. Your message is now published into the specified channel on Cloud. This method internally makes an HttpWebRequest to carryout the operation. More details on the implementation of Publish can be found in PubNub C# API.
Server publishes messages. So, eventually our client should listen to messages. How does this clinet look like? Youc an see the Subscribing client component in the image below.
There is only one input required to subscribe for messages, Channel Name. There is a button at the top, "START SUBSCRIPTION" which will tune client into the specified Channel and start listening to the messages on that Channel using PubNub C# API.
As and when a message is published from the server on this channel, client grabs it immediately and displays it on the text box.
The OnClick() of "START SUBSCRIPTION" button is defined in the code snippet below.
private void StartSubscriptionOnClick(object sender, EventArgs e)
{
pubnub = new Pubnub(PUBLISH_KEY, SUBSCRIBE_KEY, SECRET_KEY, false);
channel = txtChannel.Text;
// Extract History on successful Connection
List<object< history = pubnub.history(channel, 10);
ExtractHistory(history);
// Listen to the channel and update the UI
this.backgroundWorker1.RunWorkerAsync();
}
Subscribing for messages requires 3 steps:
Step 1. Instantiate the PubNub API. You need to have 3 keys namely - PUBLISH_KEY, SUBSCRIBE_KEY & SECRET_KEY to instantiate PubNub API as in teh above code snippet.
Step 2. Retrieve History of messages. Make a call to the "History(<Channel>, <No. of past messsages>)" method on PubNub API. You need to pass the 'Channel Name' and the 'No. of past messsages' as parameters to this method as shown in the above snippet. You can see the History messages retrieved in the image of the Client.
Step 3. Tune on to a specific channel and start listening to messages. Make a call to the "Subscribe(<Channel>, <Callback Method>)" method on PubNub API. You need to pass the 'Channel Name' and the 'Callback Method(delegate)' as parameters to this method as shown in the below snippet.
This method starts listening on the specified channel continously. So, it is executed on a separate background thread as shown in the above snippet - "this.backgroundWorker1.RunWorkerAsync();".
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
pubnub.subscribe(channel, new Pubnub.Procedure(SubscriptionUpdates));
}
History & Subscribe methods internally makes an HttpWebRequest to carryout the operation. More details on the implementation of these methods can be found in PubNub C# API. There can be multiple clinets listening to the same channel or different channels where the server will publish the messages.
Click here to download the source code for this solution.
Author:
Kris.Ram, Integration Specialist
Minnesota, USA