Publish and Subscribe Overview
API Keys
subscribe_key
and publish_key
. If a client will only subscribe, and not publish, then the client only need to initialize with the subscribe_key
. For clients who will be publishing only, or publishing and subscribing (a client can both publish and subscribe), it will need to initialize with both the subscribe_key
and the publish_key
.publish_key
to clients that will publish (send) data to your application over the PubNub network. A read-only client for example would not need to initialize with this key.Although a secret_key is also provided to you along with your publish and subscribe keys in the admin portal, it is not required for plain-old publish and subscribe. You'll only need the secret_key if you are using PAM functionality, which we discuss more in the PAM Feature Tutorial. |
Messages Overview
Message Design ConsiderationsWhen creating a message, keep these limits in mind:
Keeping your messages < 1.5KB in size will allow them to fit into a single TCP packet! For further details please check: https://support.pubnub.com/support/discussions/topics/14000006322 |
Channel Design Patterns: Unicast vs Multicast

public
(AKA system
, global
, or admin
) channel is used for global communications amongst all clients. For example, building off our previous example, while a user can speak to any other user on their private
channel, since each client in the application is listening on their private channel AND the public channel, they can receive on either. When receiving a message on the public
channel, it may or may not be relevant for that particular receiving client -- to get around this, the client can filter on some sort of key
, allowing them to selectively process messages with specific interest to them.
Channel Name Design Considerations
- comma:
,
- slash:
/
- backslash:
\
- period:
.
- asterisk:
*
- colon:
:
Data Streams Code Samples
Publishing and Subscribing requires only a few simple-to-use APIs:
- Include the PubNub library
Pubnub()
- instantiate a PubNub instance.subscribe()
- additively subscribe to a specific channel.publish()
- send a message on a specific channel.unsubscribe()
- additively unsubscribe to a specific channel.
Include the PubNub library
- Add new project with java as the root
- Set the new project source as java/srcPubnubApi
- Add
Pubnub-CodeNameOne-3.7.11.cn1lib
and libs/*.jar as project libraries
import com.pubnub.api.*;
import org.json.*;
Initialize the API
subscribe_key
to initialize the instance. If this instance will be subscribing and publishing, you must also include the publish_key
parameter.Pubnub pubnub = new Pubnub("demo", "demo");
Publishing and Subscribing to a Channel
publish()
and subscribe()
methods are pretty simple to use. For both publish()
and subscribe()
, the channel attribute defines the channel in use.subscribe()
the message callback is where received messages are called-back to: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());
}
}
);
NOTE: During your application's lifecycle, you can call subscribe() repeatedly to additively subscribe to additional channels. |
publish()
, the message attribute contains the data you are sending.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);
The above code demonstrates how to subscribe, and how to publish. But what if your use-case requires that client instance not only subscribes and publishes, but also that its guaranteed to start publishing only AFTER it's successfully subscribed? -- In other words, you want to guarantee it receives all of its own publishes?
The JavaScript Web client SDK, like many of the PubNub SDKs, is asynchronous -- publish()
can, and most likely will, fire before the previously executed subscribe()
call completes. The result is, for a single-client instance, you would never receive (via subscribing) the message you just published, because the subscribe operation did not complete before the message was published.
To get around this common case, we can take advantage of the optional connect
callback in the subscribe method:
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() {});
}
});
Unsubscribing from a Channel
unsubscribe()
from the channel.pubnub.unsubscribe("my_channel");
subscribe()
, unsubscribe()
can be called multiple times to successively remove different channels from the active subscription list.