JOIN MY SPONSORS

Join & Leave Events

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', ({ total }) => {
  console.log(`Someone joined! ${total} online`)
})

channel.on('leave', ({ total }) => {
  console.log(`Someone left. ${total} 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, total }) => {
  console.log(`${alias} (${uid}) joined — ${total} online`)
})

channel.on('leave', ({ uid, alias, total }) => {
  console.log(`${alias} left — ${total} 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
totalnumberTotal users now on the channel
selfboolean?Present on your own join event
usersArray?List of announced users (only with list: true, join only)
uidstring?User ID (always on your own join, otherwise with announce: true)
aliasstring?Display name (always on your own join if set, otherwise 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: List Usersget a snapshot of everyone in the channel.