This is the 15th in series of posts leading up Node.js Knockout, and covers using PubNub in your node app.

PubNub lets you connect mobile phones, tablets, web browsers and more with a 2 Function Publish/Subscribe API (send/receive).

HTML Interface

If you are building HTML5 Web Apps, start by copying and pasting the code snippet below. If not, skip to Other Languages.

<div pub-key="demo" sub-key="demo" id="pubnub"></div>
<script src="http://cdn.pubnub.com/pubnub-3.1.min.js"></script>
<script>(function(){

    // Listen For Events
    PUBNUB.subscribe({
        channel  : "hello_world",      // Channel
        error    : function() {        // Lost Connection (auto reconnects)
            alert("Connection Lost. Will auto-reconnect when Online.")
        },
        callback : function(message) { // Received An Event.
            alert(message.anything)
        },
        connect  : function() {        // Connection Established.

            // Send Message
            PUBNUB.publish({
                channel : "hello_world",
                message : { anything : "Hi from PubNub." }
            })

        }
    })

})();</script>

Other Languages

Follow the instructions linked below to use PubNub APIs from other programming languages: Node, Ruby, PHP, Python, Perl, Erlang and more programming languages on GitHub.

Blog comments powered by Disqus