Integrating PubNub With Grails

17 / Feb / 2015 by Sumant Thakur 0 comments

PubNub is a real-time push service provider for different platform and by using PubNub with Grails we can create push notification system similar to Facebook.

My use case was that I needed to create a notification system where user should be notified for different activities which is required for the user to be known.

A notification is about something being changed by someone and reported to the user. Here is a normalized schema. You need to notify certain users about changes. So it’s per-user notifications, which means that if there were 100 users involved, then 100 notifications are generated.

notification

To get started with PubNub first of all go to www.pubnub.com and create your account & activate basic sandbox plan.

Clone javase-sdk from PubNub git repo https://github.com/pubnub/java.git and put all downloaded JAR files into grails-app’s lib directory.

Initiating PubNub

Put this code in any of .groovy file (Publisher.groovy) inside application.

Pubnub pubnub = new Pubnub(publishKey, subscribeKey)

publishKey:- Your PubNub publisher key

subscribeKey:- Your PubNub subscriber key

Publishing to PubNub

Put this code also in the same groovy file in which PubNub is initiated

pubnub.publish(channelName, message, callback)

channelName:- This is string value for the user for whom this notification is intended.

Note:- Keep the channel name unique and identifiable for the user because it is also used to subscribe for channel data.

message:- This is a JSON Object which will be published to the user channel

callback:- This is a function which handles the response from PubNub

eg:-


Callback callback=new Callback() {

@Override

public void connectCallback(String channel, Object message) {

println(message)

}

@Override

public void disconnectCallback(String channel, Object message) {

println(message)

}

public void reconnectCallback(String channel, Object message) {

println(message)

}

@Override

public void successCallback(String channel, Object message) {

println(message)

}

@Override

public void errorCallback(String channel, PubnubError error) {

println(message)

}

}

Now it’s time to receive the notifications published to user channel, for this we will require PubNub javascript-sdk

include <script src=”http://cdn.pubnub.com/pubnub-3.7.1.min.js“></script> in your html

initiate PubNub


<script>

$(document).ready(function(){

var pubnub = PUBNUB.init({

publish_key: publishKey,

subscribe_key: subscribeKey

});

pubnub.history({

channel:channelName,

count:50 //by default it is 100

callback:function(m){

//Here you can use Jquery to append content to web page

console.log(m)

}

});

pubnub.subscribe({

channel:channelName,

callback: function (m) {

//Here you can use Jquery to append content to web page

console.log(m)

}

});

}

</script>

publishKey, subscribeKey and channel name should be same as used for publishing to the pubnub.

pubnub.history() loads last 100 notification from user channel by default, the number of notifications to be retrieved can be specified by using count key.

pubnub.subscribe() keeps listening to the user channel for any new notification.

Hope this was helpful and informative.

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -