Javascript Beginner Tutorial

This is as complex as it gets:

	// PUBNUB.subscribe() - LISTEN
	PUBNUB.subscribe({
	    channel  : "my_channel",
	    callback : function(message) { alert(message) }
	});

	// PUBNUB.publish() - SEND
	PUBNUB.publish({
	    channel : "my_channel",
	    message : "Hi."
	});

This tutorial will demonstrate two primary API functions in PubNub: PUBNUB.publish() and PUBNUB.subscribe(). Let's start with PUBNUB.publish().

PUBNUB.publish(args)

This function sends a message. Publish is the send function. The message may be an array, object or string.

	// Run the Publish Function
	PUBNUB.publish({
		// Set channel to 'my_channel'
	    channel : 'my_channel',

		// Set message to a string
	    message : 'Hello World'
	});

This will send a message to the 'my_channel' channel. Anyone listening to this channel will receive "Hello World". Note that all channels are namespaced exclusively to your account. Other developers will not be able to send messages to your 'my_channel' channel. Even if the other crazy developer attempts to send a message on the same channel, all messages go to her namespace instead.

When sending a message, the message can be an array, object or string.

1,800 Bytes is the maximum message length. If you go over this value, the message will not be sent. If you force this with trickery, then truncation will occur which is not desirable.

Now lets adventure into the hidden pyramid ski resort zone of PUBNUB.subscribe().

PUBNUB.subscribe(args)

This function listens for messages. Subscribe is the receive function. This function may receive multiple messages simultaneously.

	// Run the Subscribe Function
	PUBNUB.subscribe({
	    // Listen on channel 'my_channel'
	    channel  : "my_channel",
		
	    // Set Callback Function when Messages Received
	    callback : function(message) { alert(message) }
	});

The Subscribe Response returns the exact message that was published from the PUBNUB.publish() function. If the message was sent as an object or array, then the message will still be an object or array when received.

PUBNUB.subscribe() is simple. It will listen on a channel, and execute your callback when messages are received.

Important to note that each response will include a single message. No need to iterate over an array like in the old API.

There is no guarantee that all messages published will be received in the same order that they were sent. This is because the PubNub communication cluster may respond from different locales around the world. It is important that your PubNub app be capable of dealing with unordered messages.

"Hello World" without comments:

	PUBNUB.subscribe({
	    channel  : "my_channel",
	    callback : function(message) { alert(message) }
	});

	PUBNUB.publish({
	    channel : 'my_channel',
	    message : 'Hello World!!!'
	});

That's what I call Simple! Right? Click the button above to run the code and see it in action.

PubNub JS Include

You must include the PubNub JS Include in your page to use the PUBNUB object. Click the link above to get your include link.

*** It is required that you always include the PubNub JavaScript Push API from cdn.pubnub.com domain. It is not recommended to download the source and host the file on a private server. There is a good reason for this. The API is constantly being improved! There are security risks that will prevent your app from running in the future. Make sure to keep up to date by always linking directly to cdn.pubnub.com domain. There are too many scenarios to mention and many which haven't been thought of. Make sure to link directly.

Of course there may be several reason for downloading your source and hosting it on your own web server. Make sure to keep updated.