PubNub JavaScript APIs

Presence

Presence In a Nutshell

Presence allows you to track who (or which device, if you are building an Internet of Things application) is online and offline, and the status of users/devices in realtime. (API)

Demo

See the previous section to see How to Play Tic-Tac-Toe.

You (X):

  1. Stay on this browser
  2. Play when your opponent is ready

Your opponent (O) should be playing from this URL:

Presence in Action

This game won't start until two players are connected at the same channel. Presence detection makes it possible to track when the second player logs on.

0 players
Game ID:
You:
It is your turn!

Presence API Explained

Presence allows you to detect more than just a number of users online. It triggers the following presence events against the channels too. They are, Join, Leave, and timeout-

Observe the Join action, by joining to the game. When you open an another browser window or tab to access the same game ID, you should see the presence indication each time a new player joining (although any players who joine after the 2nd player cannot play the game).

Observe the Leave action, by manually leaving the game. Click "Quit" button below to unsubscribing from the game channel.

Observe Timeout action, by closing the browser tab or window. It happens when the browser loses its socket connection, and it can take up a few minutes to timeout. It happens when you are accessing on mobile and loses the connectivity, too.