SelectJava SE SDK 3.7.11

These docs are for version 3.7.11 of the Java SDK. To check out the docs for the latest version Click Here.
 

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

PubNub 3.x for Java will encounter End Of Life (EOL) March 1st, 2017. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

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.11.jar file into your project's libs directory.
(To use the debug version of Pubnub, simply copy the Pubnub-StandardEdition-Debug-3.7.11.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.11</version>
    </dependency>
</dependencies>

dependencies {
    ....
    compile 'com.pubnub:pubnub:3.7.11'
   //'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.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
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 you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
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 you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
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");