Get Code: github
Place this file in
/cloud/*
folder of your app.How to Get It: Source
Hello World
Install PubNub using the procedure defined under How to Get It.Example
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)
}
});
});
Copy and paste examples:
Init
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 |
// 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! |
Time
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
Publish a message to a channel:
pubnub.publish({
channel : 'my_channel',
message : 'Hello from the PubNub Javascript SDK!',
callback : function(m){
console.log(m)
}
});
Here Now
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)
}
});
History
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
});