Reconnection
Activity tracking
Mesh includes intelligent activity tracking that:
- Monitors input activity through mouse, keyboard, and touch events
- Detects when a tab becomes inactive
- Tries to determine if the connection might have been affected by browser throttling
- Automatically forces a reconnection when necessary
- Ensures all subscriptions are properly restored
This is particularly useful for handling browser behavior where tabs that go inactive have their JavaScript execution throttled, which can cause WebSocket connections to appear active from the client’s perspective but be considered dead by the server.
Configuration
You can customize the reconnection behavior when creating a client:
import { } from "@mesh-kit/client";
const = new ("ws://localhost:8080", {
: true, // Enable auto-reconnection (default: true)
: 2000, // ms between reconnection attempts (default: 2000)
: 5, // give up after 5 tries (default: Infinity)
: 30000, // ms to wait for ping before considering connection dead (default: 30000)
: 1, // number of missed pings before reconnecting (default: 1)
});
Subscription restoration
When a client reconnects after a disconnection or tab inactivity, the following are restored:
- Room memberships - All rooms the client had joined
- Room presence subscriptions - All presence callbacks for rooms
- Channel subscriptions - All channel subscriptions with their callbacks and history limits
- Record subscriptions - All record subscriptions with their callbacks and modes
- Collection subscriptions - All collection subscriptions with their callbacks
Last updated on