This section outlines how to set up and initialize a PubNub instance, identify a user, and handle disconnection and reconnection.
Start by including the PubNub SDK in your code.
For Swift, you'll need to install CocoaPods gem and include the PubNub framework in your Podfile. Refer to this guide for detailed instructions.
subscribeKey when you initialize PubNub from your application. You can find your keys on your PubNub Admin Dashboard.
If a client will only receive messages, it only needs the
subscribeKey. If a client will send messages as well, it needs both the
publishKey and the
Setting a UUID for each user
You need to specify a UUID when you initialize PubNub. The UUID should be unique for each user, and should remain unchanged for the lifetime of that user. If your application doesn't set a UUID, the SDK automatically generates a random UUID on every login.
For more information on users and user management, refer to the Users overview.
Disconnecting from PubNub
Whenever possible, use
unsubscribeAll() (in Swift,
unsubscribeFromAll()) to disconnect from PubNub before a user quits your application or moves it to the background.
Reconnecting to PubNub
The Swift and Java SDKs (and most of the other PubNub SDKs) automatically reconnect if you enable the reconnection policy flags when you initialize the PubNub instance.
autoNetworkDetection flag, and catches up missed messages from the queue if you enable the
restore flag when you initialize PubNub.
PNNetworkDownCategorywhen the network connection disappears
PNNetworkUpCategorywhen the network connection reappears
pubnub.reconnect() when you receive the