WANRING: This content is OLD!
Please visit https://github.com/pubnub/pubnub-api

JavaScript API Advanced

The PUBNUB object is a Publish + Subscribe Push Framework for JavaScript.

Download JavaScript Push API

PubNub is designed for developers and businesses that have event-driven needs within their applications and games that require a flexible, reliable, cost-effective messaging solution that can scale seamlessly. It is a web service that makes it easy to set up, operate, and send notifications from the cloud. PubNub can be highly customized by developers to meet a wide range of application needs. PubNub allows applications and end-users on different devices to receive notifications instantly.

Developers can get started by using just two APIs: PUBNUB.subscribe(), and PUBNUB.publish(). All messages instantly become compressed JSON. PubNub is designed to meet the needs of the largest and most demanding applications and games, allowing an unlimited number of messages published at any time. PubNub provides interface mechanisms to ensure that messages are secured against unauthorized access. Developers can chose to restrict who can publish or subscribe. APIs available in JavaScript, iPhone, Android, Flash, Ruby, Python and more which all deliver bidirectional Real-time messaging service in the cloud.

This is the Advanced PubNub 3.0 JavaScript Real-time Cloud Push API turorial. To start with a basic intro, read the PubNub Developer Intro Tutorial Hello World.

Compatibility

iPhone, iPad, Android, BlackBerry, Windows, Symbian. FireFox, Safari, Opera, Chrome, Flock, IE. Cross Browser, Cross Domain, Mobile.

Subscribe

    // PUBNUB.subscribe( options )
    PUBNUB.subscribe({
        channel  : "my_channel",
        callback : function(message) { console.log(message) },
        error    : function(e) {
            // Do not call subscribe() here!
            // PUBNUB will auto-reconnect.
            console.log(e);
        }
    });


* Open FireBug Console for More Info

Publish

    // PUBNUB.publish( options )
    PUBNUB.publish({
        channel  : "my_channel",
        message  : "Hello World",
        callback : function(info) {

            // info[0] == 1 for success
            // info[0] == 0 for failure

            // info[1] == "D" for "Demo Success" or
            // info[1] == "S" for "Success with Valid Key" or
            // info[1] == "Error..." with reason of failure.

            // if the respons is an error, do not re-publish.
            // the failed publish will not re-send.

            console.log(info);
        }
    });


* Remember to TEST PUBNUB.subscribe() first

Additional functions for your application. UnSubscribe is available in PubNub JavaScript Push API Use the History command to recall previous messages published. UUID for generating a Universally Unique ID. Use the Time function for synchronized time around the globe.

Unsubscribe

    // PUBNUB.unsubscribe( options )
    PUBNUB.unsubscribe({ channel : 'my_channel' });

History

    // PUBNUB.history( options, callback )
    PUBNUB.history( {
        channel : 'my_channel',
        limit   : 10
    }, function(messages) {
        // Shows All Messages
        console.log(messages);
    } );


* Remember to TEST PUBNUB.publish() first!
* Open Console for More Info

UUID

    // PUBNUB.uuid( callback )
    PUBNUB.uuid(function(uuid) {
        console.log(uuid);
    });

Time

    // PUBNUB.time( callback )
    PUBNUB.time(function(time) {
        console.log(time);
    });

DOM + PUBNUB

The following provide the fastest possible access to the DOM. Focus is on performance and simplicity. Anti-jQuery syntax provides increased performance; especially on mobile phones. However, feel free to use jQuery and other libraries with PUBNUB.

jQuery-style $

    // PUBNUB.$(id)
    var html_node = PUBNUB.$('id-of-html-element');
    var some_div  = PUBNUB.$('some-div');
    

Bind

    // PUBNUB.bind( type, node, callback )
    PUBNUB.bind(
        'click',
        PUBNUB.$('id-of-html-element'),
        function(element) {
            console.log('clicked');
            console.log(element);
        }
    );

    PUBNUB.bind(
        'mousedown',
        PUBNUB.$('some-div'),
        function(element) {
            console.log('clicked');
        }
    );

Search

    // PUBNUB.search(element_type)
    var all_divs  = PUBNUB.search('div');
    var all_links = PUBNUB.search('a');

Attr

    // PUBNUB.attr( node, attribute, value )

    var node = PUBNUB.$('some-div-id');

    // Get
    var href = PUBNUB.attr( node, 'href' );
    // Set
    PUBNUB.attr( node, 'href', '/blog' );

CSS

    // PUBNUB.css( node, styles )
    PUBNUB.css( PUBNUB.$('some-div-id'), {
        top   : 100,
        left  : 100,
        color : "#ff0000"
    } );

Supplant

    // PUBNUB.supplant( template, data )
    var text = PUBNUB.supplant( 'Hello {name}!', { name : 'John' } )
    // Hello John!

There are only two functions you need to know: Publish() and Subscribe(). JavaScript Push API focuses on speed and usability.

Click to see your JS Include