Build

Pub/Sub Messaging with Google Go Programming Language

Michael Carroll on Dec 17, 2014
Pub/Sub Messaging with Google Go Programming Language

This tutorial will show you how to create an Google App Engine web application using the PubNub Go Programming Language SDK. We will create an app to demonstrate the functionality of PubNub real-time Pub/Sub messaging using the PubNub Golang SDK. Specifically, the example will let you subscribe to a PubNub channel, then publish and receive messages on it.

This will enable you to bidirectionally send data (both unicast and broadcast) between any number of devices.

Prerequisites:

  • Install Eclipse IDE.
  • Download and install Google App Engine SDK for Go.
  • Install golang plugin for Eclipse.
  • Clone the PubNub Go SDK library. We'll use the code from the directories gae-exampleand gae
  • The SDK has a dependency on Gorilla web toolkit for sessions. You need to download Gorilla web toolkit from the git repo using go get github.com/gorilla/sessions and copy it to github.com/gorilla/sessions in your project maintaining the folder structure.

Now let's get started!

Subscribe to PubNub Channel Using Javascript

Subscribe/presence/unsubscribe are implemented on the client end using the PubNub Javascript SDK. To use the SDK, we need to include the JavaScript SDK in the code:

Initialize with Publish & Subscribe Keys in Javascript. The uuid for the application is sent from the server.

  • Publish Key: Your Publish Key.
  • Subscribe Key: Your Subscribe Key.
  • Cipher: Cipher key for encryption, can be empty.
  • SSL: true or false, to enable or disable SSL.
  • UUID: can be empty or you can set a custom UUID.

Subscribe to a PubNub channel in Javascript

We need to subscribe to a channel to receive all the messages sent on the channel. This is done by using the following code:

Here channel is the name of the PubNub channel which we are subscribing to. The subscribe method asks for 2 callback methods, one for receiving the messages and the other to receive the connect status.

Publish a message using the PubNub’s GO GAE SDK

Publish runs on the app engine. A button input type is created on the HTML page. This button is linked to a javascript function which asks the user to enter the message to publish. When entered the query is posted using jQuery to the server. This calls the publish handler on the server.
The following code is used on the server to create the publish feature.

Import the dependency

  • github.com/pubnub/go/gae/messaging is the reference to PubNub’s GAE SDK using GO.

Initialize a new PubNub instance

To use the PubNub SDK we need to instantiate its instance. The instantiation is done my calling the new method of the messaging package.

The New constructor takes the following parameters:

  • AppEngine context: reference of the context from the app engine.
  • UUID: can be empty or you can set a custom UUID.
  • http.ResponseWriter: reference of the ResponseWriter from the mux.
  • Publish Key: Your Publish Key.
  • Subscribe Key: Your Subscribe Key.
  • Secret Key: Your Secret Key.
  • Cipher: Cipher key for encryption, can be empty.
  • SSL: true or false, to enable or disable SSL.

Publish

We've already covered the implementation of Init in the above section “Initialize a new PubNub instance”,  and handleResult in the next section below.

In the Publish method you need to give same PubNub channel name as the one subscribed (using Javascript). And the message to publish.

The callbackChannel is called when we the message is successfully posted. The response is something like this:

The errorChannel is called when there is an error publishing the message. Like incase of a timeout when publishing the message.

handleResult

This function is a utility function used in the examples below to handle the non subscribe/presence response. You would want to adapt it to your own needs. This function reads both the errorChannel and callbackChannel until we get a response on either of the go channels.

When the message is published, the same message will be displayed in the text area on the browser as we are subscribed to the channel.

Let's run the code!

Configuration

  • Run the following command on the terminal/command prompt.

This command imports the PubNub’s Go SDK to the path <Go-Workspace-Folder>/src/pubnub/go

Run the example:

  • Run the PubNub GAE example using GO on the dev app server using the command <PATH-to-go_appengine/dev_appserver.py> <Path-to-PubNub-GAE-Folder> –port <port-number-of-your-choice>
  • Run http://localhost:<port-number-of-provided-in-the-command-above>
  • On run click the Connect button to connect to the server.
  • To subscribe to a channel click the button Subscribe using JS SDK. This will ask for a channel name, on providing a channel name the application will subscribe to the channel.
  • Publish a message by clicking the publish button. It will ask for a channel name and the message to publish. On providing both the values the message will be published on the channel.
  • The callback message from the Publish call will be displayed in the message area on the browser.
  • And so will the message that is received in the Subscribe callback.

Links:

The complete code is available here:

And, the full code: