Skip to main content

Overview

Presence allows you to track which users are currently online and synchronize state across clients in real-time. It’s perfect for showing “who’s online” indicators, collaborative features, and multiplayer games.
Presence automatically handles connection and disconnection events, removing users when they leave or lose connection.

Key Features

  • Automatic cleanup: Users are automatically removed when they disconnect
  • Conflict resolution: Built-in CRDT ensures consistent state across clients
  • Scalable: Efficiently handles thousands of concurrent users
  • Flexible state: Track any JSON-serializable user data

Basic Usage

Track Your Presence

Add yourself to a channel’s presence:

Get Current Presence State

Retrieve all users currently present:
The presence state is an object where keys are user identifiers:

Complete Example

Here’s a real implementation based on the nextjs-auth-presence example in the source:

Presence Events

Presence emits three types of events:

Sync Event

Fired when the presence state is synchronized:

Join Event

Fired when new users join:

Leave Event

Fired when users leave or disconnect:

Advanced Patterns

Update Presence State

Update your presence data without reconnecting:

Collaborative Editing Example

Track cursor positions and selections in a collaborative editor:

Multiplayer Game Lobby

Track players in a game lobby:

Private Channels with Presence

Combine presence with authorization for private spaces:

Combining Presence with Broadcast

Use both features together for rich collaborative experiences:

Performance Considerations

Throttle Presence Updates

Avoid updating presence too frequently:

Limit Presence State Size

Keep presence data small for better performance:

Clean Up on Unmount

Always unsubscribe when components unmount:

Troubleshooting

User Not Appearing in Presence State

  1. Verify subscription status:
  1. Check if track() was called:
  1. Ensure unique presence keys:

Users Not Being Removed

Presence automatically removes users after 60 seconds of inactivity. If a user appears stuck:
  • Check network connectivity
  • Verify the channel is still subscribed
  • Check browser console for errors

Next Steps

Broadcast

Send ephemeral messages between clients

Postgres Changes

Listen to database changes in real-time