Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Pull request Compare This branch is 1 commit ahead, 1239 commits behind pubnub:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

..
Failed to load latest commit information.
.settings
src
.gitignore
README
pom.xml

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
        "",      // CIPHER_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) {}

    // Create HashMap parameter
	HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");		// Channel Name
	args.put("message", message);			// JSON Message
    
    // Publish Message
    JSONArray info = pubnub.publish( args );

    // 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();
    
    // Create HashMap parameter
    HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");			// Channel Name
	args.put("callback", message_receiver);		// Receiver Callback Class
    
    // Listen for Messages (Subscribe)
    pubnub.subscribe( args );


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

	// Create HashMap parameter
    HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");		// Channel Name
	args.put("limit", 1);					// Limit
	
    // Get History
    JSONArray response = pubnub.history( args );

    // 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.