PubNub Java SE SDK 3.7.10

 

PubNub 4.0 for Java is our latest and greatest! Please click here for our PubNub 4.0 for Java docs and SDK.

If you have questions about the PubNub for Java SDK, please contact us at support@pubnub.com.

To use Pubnub, simply copy the Pubnub-StandardEdition-3.7.10.jar file into your project's libs directory.
(To use the debug version of Pubnub, simply copy the Pubnub-StandardEdition-Debug-3.7.10.jar file into your project's libs directory.)

You need to also add these dependencies:

json-20090211.jar

slf4j-api-1.7.15.jar
<dependencies>
...
    <dependency>
        <groupId>com.pubnub</groupId>
        <artifactId>pubnub</artifactId>
        <version>3.7.10</version>
    </dependency>
</dependencies>
dependencies {
    ....
    compile 'com.pubnub:pubnub:3.7.10'
   //'com.pubnub:pubnub-debug:3.7.+' For the debug version
}



Add PubNub to your project using one of the procedures defined under How to Get It.
 import com.pubnub.api.*;
 import org.json.*;
 Pubnub pubnub = new Pubnub("demo", "demo");
 
 try {
   pubnub.subscribe("my_channel", new Callback() {
       @Override
       public void connectCallback(String channel, Object message) {
           pubnub.publish("my_channel", "Hello from the PubNub Java SDK", new Callback() {});
       }

       @Override
       public void disconnectCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                      + " : " + message.getClass() + " : "
                      + message.toString());
       }
 
       public void reconnectCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                      + " : " + message.getClass() + " : "
                      + message.toString());
       }
 
       @Override
       public void successCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : " + channel + " : "
                      + message.getClass() + " : " + message.toString());
       }
 
       @Override
       public void errorCallback(String channel, PubnubError error) {
           System.out.println("SUBSCRIBE : ERROR on channel " + channel
                      + " : " + error.toString());
       }
     }
   );
 } catch (PubnubException e) {
   System.out.println(e.toString());
 }
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this Java instance.
 
For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.
Pubnub pubnub = new Pubnub("demo", "demo");
 
If you no longer need to use the Pubnub instance, do not forget to explicitly stop it's running threads using shutdown() method.
Call time() to verify the client connectivity to the origin:
 Callback callback = new Callback() {
   public void successCallback(String channel, Object response) {
     System.out.println(response.toString());
   }
   public void errorCallback(String channel, PubnubError error) {
   System.out.println(error.toString());
   }
 };
 pubnub.time(callback);
Subscribe (listen on) a channel (it's async!):
pubnub.subscribe("my_channel", new Callback() {

      @Override
      public void connectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : CONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : " + channel + " : "
                     + message.getClass() + " : " + message.toString());
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
          System.out.println("SUBSCRIBE : ERROR on channel " + channel
                     + " : " + error.toString());
      }
    }
  );
Publish a message to a channel:
Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
	System.out.println(error.toString());
  }
};
pubnub.publish("my_channel", "Hello from the PubNub Java SDK!" , callback);
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
    public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
    }
    public void errorCallback(String channel, PubnubError error) {
        System.out.println(error.toString());
    }
};
pubnub.hereNow("my_channel", callback);
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
    @Override
    public void connectCallback(String channel, Object message) {
       System.out.println("CONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void disconnectCallback(String channel, Object message) {
        System.out.println("DISCONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void reconnectCallback(String channel, Object message) {
        System.out.println("RECONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void successCallback(String channel, Object message) {
        System.out.println(channel + " : "
                + message.getClass() + " : " + message.toString());
    }

    @Override
    public void errorCallback(String channel, PubnubError error) {
        System.out.println("ERROR on channel " + channel
                + " : " + error.toString());
    }
};

try {
    pubnub.presence("my_channel", callback);
} catch (PubnubException e) {
    System.out.println(e.toString());
}
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
	System.out.println(error.toString());
  }
};
pubnub.history("history_channel", 100, true, callback);
Stop subscribing (listening) to a channel.
pubnub.unsubscribe("my_channel");

Check out PubNub's other Java-based SDKs, such as Java, Android, Blackberry, CodenameOne, GWT, J2ME, Scala.

loading...
Loading...