!!!!!!!!!!
PubNub Java SDK development has moved to the PubNub Kotlin SDK repository.
This repository is no longer being updated.
!!!!!!!!!!
PubNub Java-based SDKs for Java / Android
This is the official PubNub Java SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
Get keys
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
Configure PubNub
-
Integrate the Java SDK into your project:
-
for Maven, add the following dependency in your pom.xml
:
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-gson</artifactId>
<version>6.4.5</version>
</dependency>
-
for Gradle, add the following dependency in your gradle.build
:
implementation 'com.pubnub:pubnub-gson:6.4.5'
-
Configure your keys:
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUserId"));
pnConfiguration.setSubscribeKey("mySubscribeKey");
pnConfiguration.setPublishKey("myPublishKey");
PubNub pubnub = new PubNub(pnConfiguration);
Add event listeners
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
switch (status.getOperation()) {
case PNSubscribeOperation:
case PNUnsubscribeOperation:
switch (status.getCategory()) {
case PNConnectedCategory:
case PNReconnectedCategory:
case PNDisconnectedCategory:
case PNUnexpectedDisconnectCategory:
case PNAccessDeniedCategory:
default:
}
case PNHeartbeatOperation:
if (status.isError()) {
} else {
}
default: {
}
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
String messagePublisher = message.getPublisher();
System.out.println("Message publisher: " + messagePublisher);
System.out.println("Message Payload: " + message.getMessage());
System.out.println("Message Subscription: " + message.getSubscription());
System.out.println("Message Channel: " + message.getChannel());
System.out.println("Message timetoken: " + message.getTimetoken());
}
@Override
public void presence(@NotNull PubNub pubnub, @NotNull PNPresenceEventResult presence) {
System.out.println("Presence Event: " + presence.getEvent());
System.out.println("Presence Channel: " + presence.getChannel());
System.out.println("Presence Occupancy: " + presence.getOccupancy());
System.out.println("Presence State: " + presence.getState());
System.out.println("Presence UUID: " + presence.getUuid());
presence.getJoin();
presence.getLeave();
presence.getTimeout();
presence.getHereNowRefresh();
}
@Override
public void signal(PubNub pubnub, PNSignalResult pnSignalResult) {
System.out.println("Signal publisher: " + signal.getPublisher());
System.out.println("Signal payload: " + signal.getMessage());
System.out.println("Signal subscription: " + signal.getSubscription());
System.out.println("Signal channel: " + signal.getChannel());
System.out.println("Signal timetoken: " + signal.getTimetoken());
}
@Override
public void messageAction(PubNub pubnub, PNMessageActionResult pnActionResult) {
PNMessageAction pnMessageAction = pnActionResult.getAction();
System.out.println("Message action type: " + pnMessageAction.getType());
System.out.println("Message action value: " + pnMessageAction.getValue());
System.out.println("Message action uuid: " + pnMessageAction.getUuid());
System.out.println("Message action actionTimetoken: " + pnMessageAction.getActionTimetoken());
System.out.println("Message action messageTimetoken: " + pnMessageAction.getMessageTimetoken());]
System.out.println("Message action subscription: " + pnActionResult.getSubscription());
System.out.println("Message action channel: " + pnActionResult.getChannel());
System.out.println("Message action timetoken: " + pnActionResult.getTimetoken());
}
@Override
public void file(PubNub pubnub, PNFileEventResult pnFileEventResult) {
System.out.println("File channel: " + pnFileEventResult.getChannel());
System.out.println("File publisher: " + pnFileEventResult.getPublisher());
System.out.println("File message: " + pnFileEventResult.getMessage());
System.out.println("File timetoken: " + pnFileEventResult.getTimetoken());
System.out.println("File file.id: " + pnFileEventResult.getFile().getId());
System.out.println("File file.name: " + pnFileEventResult.getFile().getName());
System.out.println("File file.url: " + pnFileEventResult.getFile().getUrl());
}
});
Publish/subscribe
pubnub.publish().channel(channelName)
.message(messageJsonObject)
.async((result, publishStatus) -> {
if (!publishStatus.isError()) {
} else {
}
});
Documentation
Support
If you need help or have a general question, contact support@pubnub.com.