JavaScript (Web)PubNub JavaScript SDK 3.16.4

 

PubNub 4.0 for JavaScript is our latest and greatest! Please click here for our PubNub 4.0 for JavaScript docs and SDK.

PubNub 3.x for JavaScript will encounter End Of Life (EOL) July 1st, 2017. All users currently on 3.x should begin migrating to 4.x.

If you have questions about the PubNub for JavaScript SDK, please contact us at support@pubnub.com.

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.16.4.min.js"></script>
    </body>
</html>
<script>
function publish() {

    pubnub = PUBNUB({
        publish_key : 'demo',
        subscribe_key : 'demo'
    })
    
    console.log("Subscribing..");
    pubnub.subscribe({                                     
        channel : "hello_world",
        message : function (message, envelope, channelOrGroup, time, channel) {
            console.log(
                "Message Received." + "\n" +
                "Channel or Group : " + JSON.stringify(channelOrGroup) + "\n" +
                "Channel : " + JSON.stringify(channel) + "\n" +
                "Message : " + JSON.stringify(message) + "\n" +
                "Time : " + time + "\n" +
                "Raw Envelope : " + JSON.stringify(envelope)
            )
        },
        connect : pub
    })

    function pub() {
        console.log("Since we're publishing on subscribe connectEvent, we're sure we'll receive the following publish.");
        pubnub.publish({
            channel : "hello_world",
            message : "Hello from PubNub Docs!",
            callback : function(m){
                console.log(m)
            }
        })
    }
};
</script>
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
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.init({
    publish_key: 'demo',
    subscribe_key: '',
    error: function (error) {
        console.log('Error:', error);
    }
})
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 realtime 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, Parse.

loading...
Loading...