This is an simple, yet thorough, example of initializing PubNub, subscribing to a channel and publishing to that channel.
connect
callback indicates that subscription to the channel was successful, so we call our pub
function which performs a publish
to the channel we just subscribed to.message
callback where we are displaying the various attributes of the received message to our browser's Console.In a real world use case, you would update your web page UI to display the received message.
<init>
(i.e. require ("pubnub")
)
Initialize an instance of PubNub to invoke operations.
Parameter | Details |
---|---|
publish_key | String - your publish key from your PubNub Admin Dashboard account |
subscribe_key | String - your publish key from your PubNub Admin Dashboard account |
subscribe
Subscribe to a channel(s) and provide a means to receive messages published to the channel(s).
Parameter | Details |
---|---|
channel | String - channel name or comma-delimited list of channel names |
message | function - the callback function that will receive messages published on the subscribe channels |
connect | function - the callback function that will be called when the subscription to the channels is successful |
publish
Publish a message to a channel which will be received by subscribers on that channel.
Parameter | Details |
---|---|
channel | String - channel name on which to send the message |
message | String - The message to publish on the channel. JSON format is recommended (do not stringify; use the JSON object) |
When using Channel Groups, you should not add or remove channels in your client side applications. This example shows adding channels to a channel group and subscribing to that channel group for simplicity sake. But in a real world scenario, you should have your server do all the add/remove of channels to/from channel groups. When you enable Access Manager, you will need the manage
permission to add/remove channels to/from channel groups and you should never grant the manage
permission to clients for security reasons. Only your server should be granted the manage
permission.
Stream Filter provides the ability to filter messages on the server before they are sent to a subscriber is a popular request. With the introduction of our v4.x SDKs, you now have the ability to do so using message meta data.
Simply enabling Storage & Playback add-on for your keys in the PubNub Admin Dashboard will result in all published messages on all channels to be stored. You can prevent a message from being stored by passing the storeInHistory
parameter as false
when the message is published, like this:
pubnub.publish(
{
message: {
'price': 8.07
},
channel: 'channel1',
storeInHistory: false // override default storage options
},
function (status, response) {
// log status & response to browser console
console.log("STATUS : " + console.log(JSON.stringify(status));
console.log("RESPONSE: " + console.log(JSON.stringify(response));
}
);
Otherwise, just omit the storeInHistory
or set to true
to store the message when it is published.