Presence

Presence tracks which users are online in a channel and what metadata they share.

How It Works

When a user joins a channel, the server automatically tracks them via Phoenix.Presence. The current state is pushed immediately after join.

Metadata

Default metadata includes:

  • online_at — Unix timestamp (seconds)
  • room — the room id, without the room: prefix
  • role — from JWT claims

You can extend metadata by customizing the after_join handler in KonetWeb.RoomChannel.

Client API

First-class presence helpers currently ship in the JavaScript SDK. The Go and Python SDKs receive the raw presence_state / presence_diff events, which you can handle with on(...) (see below).

JavaScript

// convenience: the SDK merges state + diffs and emits the current member list
channel.on('presence', (users) => {
  console.log(users) // [{ id: 'user_1', online_at, room, role }, ...]
})
 
// or read the current snapshot on demand
const users = channel.getPresence().list()

Go / Python (raw events)

ch.On("presence_state", func(p interface{}) { /* full snapshot */ })
ch.On("presence_diff",  func(p interface{}) { /* { joins, leaves } */ })
channel.on("presence_state", lambda p: print("state:", p))
channel.on("presence_diff", lambda p: print("diff:", p))

Diff Events

Presence sends lightweight diffs on change:

  • presence_state — full snapshot on join
  • presence_diff{ joins: {}, leaves: {} } on change