PubNub JavaScript SDK 3.7.14

Latest stable version


Latest stable minified version


Latest dev-only Edge Version. Not recommended for production use. For production use, please select a stable (versioned) SDK above.




You must include the PubNub JavaScript SDK in your code before initializing the client

<html>
   <body>
      <script src="http://cdn.pubnub.com/pubnub-3.7.14.min.js"></script>
   </body>
</html>

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 JavaScript instance.

 

It is not a best practice to include the secret key in client-side code for security reasons.

// Initialize the instance

var pubnub = PUBNUB({
    subscribe_key: 'demo', // always required
    publish_key: 'demo'    // only required if publishing
});

Call time() to verify the client connectivity to the origin:

// At the tone PubNub time will be...

pubnub.time(
   function(time){
      console.log(time)
   }
);

Subscribe (listen on) a channel (it's async!):

// Subscribe to a channel

 pubnub.subscribe({
    channel: 'my_channel',
    message: function(m){console.log(m)},
    error: function (error) {
      // Handle error here
      console.log(JSON.stringify(error));
    }
 });

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 that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

 // Get List of Occupants and Occupancy Count.
 
 pubnub.here_now({
     channel : 'my_channel',
     callback : function(m){console.log(m)}
 });
 

Subscribe to realt-time Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:

 

Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

 pubnub.subscribe({
     channel: "my_channel",
     presence: function(m){console.log(m)},
     message: function(m){console.log(m)}
 });
 

Retrieve published messages from archival storage:

 

Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

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
 });

Stop subscribing (listening) to a channel.

// Unsubscribe from 'my_channel'

pubnub.unsubscribe({
   channel : 'my_channel',
});

Check out PubNub's other JavaScript-based SDKs, such as AngularJS, NodeJS, Titanium, Sencha, PhoneGap, CommonJS, Web, WebWorker, SmartTV, WebOS, ImpactJS, QooxDoo, WebRTC, Parse.