Docs/Presence Channels
Docs/Channels

Presence Channels

Track online users with presence channels.

Presence Channels

Presence channels are private channels that also track who is online. Each user provides identity information through signed channel_data or connection signin, allowing you to build features like user lists, online indicators, and collaborative editing. Channel names must start with presence-.

Use Cases

  • Chat room member lists
  • Online/offline status indicators
  • Collaborative document editing
  • Live user activity feeds

Auth with User Data

Your auth endpoint must return an auth signature. Include channel_data when the connection is not signed in or when the presence channel needs its own user object:

json
{
  "auth": "APP_KEY:SIGNATURE",
  "channel_data": "{\"user_id\":\"user-123\",\"user_info\":{\"name\":\"Alice\",\"role\":\"admin\"}}"
}

The signature string includes the channel data:

plaintext
SOCKET_ID:CHANNEL_NAME:CHANNEL_DATA
js
const channelData = JSON.stringify({
  user_id: user.id,
  user_info: { name: user.name, avatar: user.avatar },
})

const stringToSign = `${socketId}:${channelName}:${channelData}`
const signature = crypto.createHmac('sha256', appSecret).update(stringToSign).digest('hex')

res.json({
  auth: `${appKey}:${signature}`,
  channel_data: channelData,
})

If the connection has already signed in and channel_data is omitted, the signed-in user object is used. When both are supplied, channel_data takes precedence for that presence channel.

Presence channels support up to 100 unique members. User IDs are limited to 128 characters and the serialized user object is limited to 1 KiB.

Accessing Members

After subscribing, access the member list:

js
channel.bind('pusher:subscription_succeeded', () => {
  channel.members.each((member) => {
    console.log('User:', member.id, member.info)
  })
})

Member Events

Presence channels automatically emit events when users join or leave:

js
channel.bind('pusher:member_added', (member) => {
  console.log('Joined:', member.info.name)
})

channel.bind('pusher:member_removed', (member) => {
  console.log('Left:', member.id)
})

Duplicate Users

If the same user_id connects multiple times:

  • member_added is not emitted again for existing users
  • member_removed is only emitted when the last connection of that user leaves