Real-Time Gaming Blog

Online Users in a Chatroom Tutorial

4 min read PubNub Staff on Jul 17, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

We'll show you how to get a list of online users in an online chatroom, creating a buddy list that updates as users join and leave the chatroom.

Waving Hand

Good News! We’ve launched an all new Chat Resource Center.

We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.

Take me to the Chat Resource Center →

In this blog post, we’ll show you how to get a list of online users in a chatroom, and update that list in real time if a user joins or leaves the chatroom.

We’ve now covered both building a multiplayer game lobby with a chatroom and the different ways we can use matchmaking to connect two different users. Here’s what we’ve covered so far:

This blog post is Part Three of PubNub Developer Evangelist Ian Jennings‘s series on creating a multiplayer game with JavaScript.

Create More Space for a Chatroom with Two Columns

You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:

We’re going to start with the code from the last example. The first thing we’ll do is divide the HTML up into columns from Bootstrap’s grid system.

First, we wrap all of the previous code in .container-fluid to start the grid system.


We add two .col-md-6 divs into a .row. Because every .row contains 12 columns, each.col-md-6 will take up half the horizontal space forming two even colums.

You can read more about the Bootstrap grid system on the Bootstrap docs.

In the first .col-md-6 we’ll create a placeholder for our list of online users. We’ll use Bootstrap’s .list-group list styling and give it an id of online-users.


We’ll copy the last chapter’s example into the second col-md-6.

PubNub Presence With Chatroom Example

Now that we have a placeholder user list, let’s fill it up with online users. PubNub Presence enables you to monitor and publish who is online in the chatroom and who is offline, and update the list when they join or leave.

We can use the PubNub JavaScript Presence API to keep track of the current users connected to the server.


The library will fire an event whenever a user joins or leaves a channel. We just need to define a “presence” parameter and a callback function to fire when something happens.

The event information in data will contain the following fields:

  • action – the type of presence event that has occurred
  • timestamp – the timestamp of when the event occurred
  • uuid – the unique user id associated with the event
  • occupancy – the current number of connected users in the channel

Here are some example events:

Back to UUID

The uuid field is a unique identifier assigned to every client that connects to PubNub. It is random by default, but you can supply your own as a setting when calling PUBNUB.init().

We’ll simply plug in the me variable from the last chapter and PubNub Presence will now show us our own usernames rather than the default random string.


And we get:

Create a List of Users

From here all we need to do is create a new div for a user when they join. We also need to make sure we properly clean up and remove it when they leave or timeout.

Chatroom Presence Demo

Put it all together and we have a list of users online in the chatroom next to the chatroom output from the last chapter. Check out the demo below, or take a look at the CodePen for getting users in a chatroom here.

See the Pen Memewarz – Who’s Online by Ian Jennings (@ianjennings) on CodePen.5248

More from PubNub

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff