A UUID (Universal Unique Identifier) is a unique string of up to 64-characters that is used to identify a client (end user, device or server) that connects to the PubNub platform.


It is required to pass a UUID when you instantiate a PubNub instance (i.e. new PubNub()). UUID should be persisted, and should remain unchanged for the lifetime of a user or a device that connects to PubNub. You can accomplish this by either passing the UUID to the client upon successful login or by persisting the UUID on the client where it can be retrieved the next time the PubNub instance is instantiated.


If you don't set a UUID yourself, PubNub SDK generates it randomly for you. So it is important that your application re-uses the UUID on each device. Not setting UUID explicitly can have significant impact on your bill if your account is setup with the Monthly Active Users (MAUs) based pricing model. If you have enabled Presence for your key set, not setting a UUID that is consistent for an end user may result in unexpected presence behavior.


Setting UUID during PubNub object initialization:


Javascript

var pubnub = new PubNub({
subscribeKey: "mySubscribeKey",
publishKey: "myPublishKey",
uuid: "myUUID"
});


Server Side UUIDs


On the server side, each server instance should also set the UUID and reuse it when they instantiate the PubNub object. The best practice wouqld be to include the UUID in the process' environment config file along with the pub/sub keys, your database connection and such.


In the Java code snippet below, the assumption is that you have a data member named pubnub that will hold an instance of PubNub. Using the singleton pattern, the getPubNub method will create and configure the instance, if it has not yet been instantiated. You may want to read the PubNub properties like publish key, subscribe key, secret key, and UUID from a configuration file or hardcode it within the server application.


Java

private PubNub pubnub;

protected void getPubNub() {
if (pubnub == null) {
pubnub = new PubNub(configPubNub());
}
return pubnub;
}

public PNConfiguration configPubNub() {
PNConfiguration pnconfig = new PNConfiguration();
pnconfg.setSubscribeKey("mySubscribeKey");
pnconfg.setPublishKey("myPublishKey");
pnconfg.setUUID("myUUID");
return pnconfig;
}


Note: Each PubNub SDK also provides a UUID generator method, for example, generateUUID(), to facilitate the generation of a UUID without managing it from a server. It is not required that you use this method to create a UUID and this format of the UUID is not required. Just consider that whatever identifier you use will be visible to other clients, so you should not use a username or email as the UUID. The UUID should be something that can be easily replaced as required without user interaction or even knowledge that it has happened.