Skip to content
Find file History
Pull request Compare This branch is 1573 commits behind pubnub:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
..
Failed to load latest commit information.
org/json
pubnub
Client.java
PubnubTest.java
README
Server.java
java-build

README

## ---------------------------------------------------
##
## YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
## http://www.pubnub.com/account
##
## ----------------------------------------------------

## ------------------------------------------
## PubNub 3.0 Real-time Cloud Push API - JAVA
## ------------------------------------------
##
## www.pubnub.com - PubNub Real-time Push Service in the Cloud. 
## http://www.pubnub.com/tutorial/java-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.

===============================================================================
PubNub Java Client API Boiler Plate
===============================================================================

-------------------------------------------------------------------------------
JavaScript: (Subscribe)
-------------------------------------------------------------------------------

    PUBNUB.subscribe( { channel : "hello_world" } , function(message) {
        console.log(JSON.stringify(message));
        alert(JSON.stringify(message));
    } );


-------------------------------------------------------------------------------
Java: (Init)
-------------------------------------------------------------------------------

    Pubnub pubnub = new Pubnub(
        "demo",  // PUBLISH_KEY
        "demo",  // SUBSCRIBE_KEY
        "",      // SECRET_KEY
        false    // SSL_ON?
    );


-------------------------------------------------------------------------------
Java: (Publish)
-------------------------------------------------------------------------------

    // Create JSON Message
    JSONObject message = new JSONObject();
    try { message.put( "some_key", "Hello World!" ); }
    catch (org.json.JSONException jsonError) {}

    // Publish Message
    JSONArray info = pubnub.publish(
        "hello_world", // Channel Name
        message        // JSON Message
    );

    // Print Response from PubNub JSONP REST Service
    System.out.println(info);


-------------------------------------------------------------------------------
Java: (Subscribe)
-------------------------------------------------------------------------------

    // Callback Interface when a Message is Received
    class Receiver implements Callback {
        public boolean execute(JSONObject message) {

            // Print Received Message
            System.out.println(message);

            // Continue Listening?
            return true;
        }
    }

    // Create a new Message Receiver
    Receiver message_receiver = new Receiver();

    // Listen for Messages (Subscribe)
    pubnub.subscribe(
        "hello_world",   // Channel Name
        message_receiver // Receiver Callback Class
    );


-------------------------------------------------------------------------------
Java: (History)
-------------------------------------------------------------------------------

    // Get History
    JSONArray response = pubnub.history(
        "hello_world", // Channel Name
        1              // Limit
    );

    // Print Response from PubNub JSONP REST Service
    System.out.println(response);
    System.out.println(response.optJSONObject(0).optString("some_key"));

Something went wrong with that request. Please try again.