Search code examples
pubnub

How to generate pubnub UUID?


I building chat application with pubnub,

I have setup everything both backend and frontend,

but in front-end, it requires another argument for UUID. Can anyone tell me where can i get the UUID? how to generate it with pubnub? Do i need to store the UUID in my datbase?

const pubnub = new PubNub({
  publishKey: 'myPublishKey',
  subscribeKey: 'mySubscribeKey',
  uuid: 'myUniqueUUID'
});

Pubnub docs it seems worst, i have not found any information regarding generating UUID, Can anyone tell me how can i resolve it?


Solution

  • PubNub UUID Management Best Practices

    The UUID should be something that, as the acronym implies, uniquely identifies the user (or device, server instance, etc.). The UUID can be whatever you see fit to accomplish this in your applications but there are some guidelines you should follow with respect to PubNub.

    This is well-documented in the PubNub Platform Documentation, so I am not going to repeat all of that here. Please review Users & Devices: Identity Management docs, under the Connections section, for the full story.

    TL;DR

    • Generate a UUID for the user when they register as a new user and store it in their user profile record in your system (or use PubNub Objects: UUIDMetadata).
    • Upon successful registration/login of a user, pass the following back to the user (the client PubNub application):
      • subscribe key
      • publish key (only need if client can send messages)
      • uuid (there are UUID generator APIs in PN SDKs - you can make your own but you should avoid using any PII as the UUID).
      • auth key (this has to do with PubNub Access Manager and not related to this post, but should be implemented for production applications)
    • use the above to init your PubNub object
    • each user/client/device should use the same UUID everytime the PubNub object is created
    • each server instance should have its own UUID. This can simply be a server instance name or identifier

    Why are UUIDs Important in PubNub?

    Again, it's all detailed in the same docs page under the UUID Impact section, but here's the highlights:

    • For billing purposes - it is used to calculate MAUs (Monthly Active Users)
    • For Presence service to work properly - uuid value is used to generate join event when a user subscribes to a channel and for all presence events and APIs.
    • For easy troubleshooting - if you ever have to contact PN Support, it makes it a lot easier to identify a user across multiple subscribes and other API calls if we can single out a UUID.

    Have a Realtime Day!!! ;)