SelectPubNub Parse SDK 3.16.5


Place this file in /cloud/* folder of your app.

Install PubNub using the procedure defined under How to Get It.
var PubNub = require('cloud/pubnub');

Parse.Cloud.define('hello_world', function (request, response) {
    var pubnub = PubNub({
        publish_key: 'demo',
        subscribe_key: 'demo'
    });

    pubnub.publish({
        channel: "hello_world",
        message: "Hello from PubNub Docs!",
        callback: function (result) {
            response.success(result);
        },
        error: function (error) {
            response.error(error)
        }
    });
});

Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this Parse instance.

 

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

// Initialize the instance

var pubnub = PUBNUB.init({
    publish_key: 'demo',
    subscribe_key: 'demo',
    error: function (error) {
        console.log('Error:', error);
    }
})
 
PubNub instantiation should be done inside Parse.com function or trigger callback!
Call time() to verify the client connectivity to the origin:
// At the tone PubNub time will be...

pubnub.time(
    function(time){
        console.log(time)
    }
);
Publish a message to a channel:
pubnub.publish({
	channel : 'my_channel',
	message : 'Hello from the PubNub Javascript SDK!',
	callback : function(m){
		console.log(m)
	}
});
Get occupancy of who's here now on the channel by UUID (requires Presence feature):
// Get List of Occupants and Occupancy Count.
 
pubnub.here_now({
	channel : 'my_channel',
	callback : function(m){
		console.log(m)
	}
});

Retrieve published messages from archival storage:

pubnub.history({
    channel : 'history_channel',
    callback : function(m){
        console.log(JSON.stringify(m))
    },
    count : 100, // 100 is the default
    reverse : false // false is the default
});