The main entry point for the SDK.
new Apinator(options: RealtimeOptions)| Property | Type | Required | Default | Description |
|---|---|---|---|---|
appKey |
string |
Yes | — | Your application key |
cluster |
string |
Yes | — | Region cluster ID (eu, us). Derives WebSocket URL as wss://ws-{cluster}.apinator.io |
authEndpoint |
string |
No | — | URL for private/presence channel authentication |
authHeaders |
Record<string, string> |
No | — | Custom headers sent with auth requests |
Opens the WebSocket connection. Returns the client instance for chaining.
Closes the WebSocket connection.
Subscribes to a channel. Returns the Channel (or PresenceChannel) instance. If already subscribed, returns the existing instance.
- Channels prefixed with
private-require authentication viaauthEndpoint. - Channels prefixed with
presence-require authentication and include member tracking.
Unsubscribes from a channel and removes all its bindings.
Returns the channel instance if subscribed, undefined otherwise.
Binds a callback to a global event (received on any channel).
Unbinds a specific callback, or all callbacks for the event if no callback is provided.
Triggers a client event on a private or presence channel. Event name must start with client-. Throws if the channel is not subscribed, not yet confirmed by the server, or is a public channel.
The socket ID assigned by the server. Available after connection.
Current connection state: "initialized" | "connecting" | "connected" | "unavailable" | "disconnected".
Represents a subscription to a channel.
Binds a callback to an event on this channel.
Unbinds a specific callback or all callbacks for the event.
Removes all event bindings from this channel.
Triggers a client event. Only works on private/presence channels. Event name must start with client-.
The channel name (read-only).
Whether the subscription has been confirmed by the server.
| Event | Data | Description |
|---|---|---|
realtime:subscription_succeeded |
varies | Subscription confirmed |
realtime:subscription_error |
unknown |
Subscription failed. Auth errors include { type: "AuthError", error: string, status: number }. |
Extends Channel with member tracking. Created automatically when subscribing to presence- prefixed channels.
Returns all current members as an array.
Returns a specific member by user ID.
The current user's presence information. This is derived from signed channel_data returned by your authEndpoint, and is set after realtime:subscription_succeeded.
Number of currently subscribed members.
| Event | Data | Description |
|---|---|---|
realtime:member_added |
PresenceInfo |
A new member joined |
realtime:member_removed |
{ user_id: string } |
A member left |
type EventCallback = (data: unknown) => void;type ConnectionState = "initialized" | "connecting" | "connected" | "unavailable" | "disconnected";interface PresenceInfo {
user_id: string;
user_info: Record<string, unknown>;
}interface AuthResponse {
auth: string;
channel_data?: string;
}interface Message {
event: string;
channel?: string | null;
data: string;
}