{"id":17225,"date":"2015-02-17T18:32:26","date_gmt":"2015-02-17T13:02:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17225"},"modified":"2016-12-19T15:34:13","modified_gmt":"2016-12-19T10:04:13","slug":"integrating-pubnub-with-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-pubnub-with-grails\/","title":{"rendered":"Integrating PubNub With Grails"},"content":{"rendered":"<p>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 <a href=\"https:\/\/www.facebook.com\/intelligrape.software\">Facebook<\/a>.<\/p>\n<p>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.<\/p>\n<p>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&#8217;s per-user notifications, which means that if\u00a0there were 100 users involved, then\u00a0100 notifications are generated.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/notification.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17230\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/notification.png\" alt=\"notification\" width=\"610\" height=\"137\" \/><\/a><\/p>\n<p>To get started with\u00a0PubNub first of all go to www.pubnub.com and create your account &amp; activate basic sandbox plan.<\/p>\n<p>Clone javase-sdk from\u00a0PubNub git repo <a href=\"https:\/\/github.com\/pubnub\/java.git\">https:\/\/github.com\/pubnub\/java.git<\/a> and put all downloaded JAR files into grails-app\u2019s lib directory.<\/p>\n<p>Initiating PubNub<\/p>\n<p>Put this code in any of .groovy file (Publisher.groovy) inside application.<\/p>\n<p>[java]Pubnub pubnub = new Pubnub(publishKey, subscribeKey)[\/java]<\/p>\n<p>publishKey:- Your\u00a0PubNub publisher key<\/p>\n<p>subscribeKey:- Your\u00a0PubNub subscriber key<\/p>\n<p>Publishing to PubNub<\/p>\n<p>Put this code also in the same groovy file in which\u00a0PubNub is initiated<\/p>\n<p>[java]pubnub.publish(channelName, message, callback)[\/java]<\/p>\n<p>channelName:- This is string value for the user for whom this notification is intended.<\/p>\n<p>Note:- Keep the\u00a0channel name unique and identifiable for the user because it is also used to subscribe for channel data.<\/p>\n<p>message:- This is a JSON Object which will be published to the user channel<\/p>\n<p>callback:- This is a function which handles the response from PubNub<\/p>\n<p>eg:-<\/p>\n<p>[java]<\/p>\n<p>Callback callback=new Callback() {<\/p>\n<p>@Override<\/p>\n<p>public void connectCallback(String channel, Object message) {<\/p>\n<p>println(message)<\/p>\n<p>}<\/p>\n<p>@Override<\/p>\n<p>public void disconnectCallback(String channel, Object message) {<\/p>\n<p>println(message)<\/p>\n<p>}<\/p>\n<p>public void reconnectCallback(String channel, Object message) {<\/p>\n<p>println(message)<\/p>\n<p>}<\/p>\n<p>@Override<\/p>\n<p>public void successCallback(String channel, Object message) {<\/p>\n<p>println(message)<\/p>\n<p>}<\/p>\n<p>@Override<\/p>\n<p>public void errorCallback(String channel, PubnubError error) {<\/p>\n<p>println(message)<\/p>\n<p>}<\/p>\n<p>}[\/java]<\/p>\n<p>Now it\u2019s time to receive the notifications published to user channel, for this we will require\u00a0PubNub javascript-sdk<\/p>\n<p>include &lt;script src=&#8221;<a href=\"http:\/\/cdn.pubnub.com\/pubnub-3.7.1.min.js\">http:\/\/cdn.pubnub.com\/pubnub-3.7.1.min.js<\/a>&#8220;&gt;&lt;\/script&gt; in your html<\/p>\n<p>initiate PubNub<\/p>\n<p>[java]<\/p>\n<p>&amp;lt;script&amp;gt;<\/p>\n<p>$(document).ready(function(){<\/p>\n<p>var pubnub = PUBNUB.init({<\/p>\n<p>publish_key: publishKey,<\/p>\n<p>subscribe_key: subscribeKey<\/p>\n<p>});<\/p>\n<p>pubnub.history({<\/p>\n<p>channel:channelName,<\/p>\n<p>count:50 \/\/by default it is 100<\/p>\n<p>callback:function(m){<\/p>\n<p>\/\/Here you can use Jquery to append content to web page<\/p>\n<p>console.log(m)<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>pubnub.subscribe({<\/p>\n<p>channel:channelName,<\/p>\n<p>callback: function (m) {<\/p>\n<p>\/\/Here you can use Jquery to append content to web page<\/p>\n<p>console.log(m)<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>&amp;lt;\/script&amp;gt;[\/java]<\/p>\n<p>publishKey, subscribeKey and channel name should be same as used for publishing to the pubnub.<\/p>\n<p>pubnub.history() loads last 100 notification from user channel by default, the number\u00a0of notifications to be retrieved can be specified by using count key.<\/p>\n<p>pubnub.subscribe() keeps listening to the user channel for any new notification.<\/p>\n<p>Hope this was helpful and informative.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":98,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17225"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/98"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17225"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17225\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}