Supporters
JOIN MY SPONSORS

Join & Leave

When a client connects to or disconnects from a channel, all other clients receive a join or leave event with the current user count.

Basic Events

By default, join and leave events include the total number of connected users:

events.js
channel.on('join', ({ users }) => {
  console.log(`Someone joined! ${users} online`)
})

channel.on('leave', ({ users }) => {
  console.log(`Someone left. ${users} online`)
})

Announcing Identity

Set announce: true and an alias to broadcast your identity when you join or leave. Other clients will see your uid and alias in the event:

announce.js
const channel = connect('my-chat', {
  as: 'Alice',       // set a display name
  announce: true,    // broadcast identity on join/leave
})

channel.on('join', ({ uid, alias, users }) => {
  console.log(`${alias} (${uid}) joined — ${users} online`)
})

channel.on('leave', ({ uid, alias, users }) => {
  console.log(`${alias} left — ${users} online`)
})

Without announce, other clients only see the updated user count — not who joined or left.

Event Payloads

FieldTypeDescription
type'join' | 'leave'The event type
usersnumberTotal users now on the channel
uidstring?User ID (only with announce: true)
aliasstring?Display name (only with announce: true)
datenumberServer timestamp

User Identifiers

Each connection gets a short, unique uid (6 characters). This identifier is:

  • Generated server-side on connection
  • Unique within the channel
  • Included in all messages from that user
  • Used for direct messages

You can set a human-readable display name using the as option (or alias).


Next: TypeScript — type your listeners, sends, and connections.