[]
Connection
is an object returned by the connect method of the Client class, representing a bidirectional connection with the server. This guide will teach you to:
Send and receive messages
Listen for events
Manage connections
Each new connection is assigned a random 20-character identifier.
This identifier is synchronized with the server-side value.
type=note
This ID is regenerated each time a reconnection occurs.
// Client-side
connection.on("connect", () => {
console.log(connection.id); // Example output: XBtqzsRMPh7MaVvaAAAB
});
connection.on("disconnect", () => {
console.log(connection.id); // undefined
});
// Server-side
server.on("connect", ({ connection }) => {
console.log(connection.id); // Example output: XBtqzsRMPh7MaVvaAAAB
});
The ID of the room to which the connection belongs.
console.log('connection.roomId'); // Example output: "room1"
Indicates whether the connection is currently connected to the server.
connection.on("connect", () => {
console.log(connection.connected); // true
});
connection.on("disconnect", () => {
console.log(connection.connected); // false
});
Use the send
method to send messages to the server.
connection.send('Hello, server!');
connection.send('Hello, server!', 'document');
connection.send({ userId: 'user1', active: true });
connection.send({ userId: 'user1', active: true }, 'my-presence');
Parameters
data
: The data to be sent.
type
(optional): The message type, used to identify the purpose or category of the message.
Example
// Send a document update
connection.send({ content: 'New paragraph' }, 'my-document');
// Send an online status
connection.send({ userId: 'user1', active: true }, 'my-presence');
// Receive and distinguish messages
connection.on('message', (data, type) => {
if (type === 'my-document') {
console.log('Document update:', data);
} else if (type === 'my-presence') {
console.log('Status update:', data);
}
});
Use the close
method to close the connection.
connection.close();
Add an event listener.
connection.on('connect', () => {
console.log('Connected to the server');
});
connection.on('message', (data, type) => {
console.log('Received message:', data, 'Type:', type);
});
Add a one-time listener that is automatically removed after being triggered.
connection.once('reconnect', (attempts) => {
console.log(`Reconnection attempt #${attempts}`);
});
Remove an event listener.
const handler = () => console.log('Error occurred');
connection.on('error', handler);
connection.off('error', handler);
Event Name | Description | Parameters |
---|---|---|
| Triggered when the connection is established | None |
| Triggered when a message is received |
|
| Triggered when the connection is disconnected |
|
| Triggered when an error occurs |
|
| Triggered during reconnection attempts |
|
| Triggered when reconnection is successful |
|
| Triggered when reconnection fails | None |
The following table lists the disconnection reasons of the disconnect
event.
Value | Description |
---|---|
| Client actively disconnected |
| Server actively disconnected |
| Client failed to respond to ping in time |
| Transport layer connection closed |
| Transport layer error occurred |