Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 02d6a26ec4
Fetching contributors…

Cannot retrieve contributors at this time

file 99 lines (90 sloc) 2.944 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
## ---------------------------------------------------
##
## YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
## http://www.pubnub.com/account
##
## USE "DEMO" INCLUDES (BELOW) FREE! WHILE DEVELOPING
##
## ----------------------------------------------------

## ----------------------------------------------------------
## PubNub 3.1 Real-time Cloud Push API - JAVASCRIPT PHONE GAP
## ----------------------------------------------------------
##
## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
## http://www.pubnub.com/tutorial/javascript-push-api
##
## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
## This is a cloud-based service for broadcasting Real-time messages
## to thousands of web and mobile clients simultaneously.

<!-- ============================= -->
<!-- INCLUDE SCRIPT PREFERED STYLE -->
<!-- ============================= -->
<script src=pubnub.min.js></script>
<script>(function(){
    // ----------------------------------
    // INIT PUBNUB
    // ----------------------------------
    var pubnub = PUBNUB.init({
        publish_key : 'demo',
        subscribe_key : 'demo',
        ssl : false,
        origin : 'pubsub.pubnub.com'
    });

    // ----------------------------------
    // LISTEN FOR MESSAGES
    // ----------------------------------
    pubnub.subscribe({
        channel : 'test',
        callback : function(message) {
            console.log(JSON.stringify(message));
        },
        error : function() {
            // The internet is gone.
            console.log("Connection Lost");
        }
    });

    // ----------------------------------
    // SEND MESSAGE
    // ----------------------------------
    setInterval( function() {
        pubnub.publish({
            channel : 'test',
            message : { example : "Hello World!" },
            callback : function(info) {
                if (info[0])
                    console.log("Successfully Sent Message!");
                else
                    // The internet is gone.
                    console.log("Failed! -> " + info[1]);
            }
        });
    }, 1000 );
})();</script>

<!-- ============================= -->
<!-- SIMPLE EXAMPLE USE PUBNUB API -->
<!-- ============================= -->
<script>
    // ----------------------------------
    // INIT PUBNUB
    // ----------------------------------
    var pubnub = PUBNUB.init({
        publish_key : 'demo',
        subscribe_key : 'demo',
        ssl : false,
        origin : 'pubsub.pubnub.com'
    });


    // -------------------
    // LISTEN FOR MESSAGES
    // -------------------
    pubnub.subscribe({
        channel : "hello_world",
        callback : function(message) { alert(message) }
    })

    // ------------
    // SEND MESSAGE
    // ------------
    pubnub.publish({
        channel : "hello_world",
        message : "Hi."
    })

</script>
Something went wrong with that request. Please try again.