On this page
The Socket instance (server-side)
Besides:
The Socket instance has a few attributes that may be of use in your application:
Socket#id
Each new connection is assigned a random 20-characters identifier.
This identifier is synced with the value on the client-side.
|
Upon creation, the Socket joins the room identified by its own id, which means you can use it for private messaging:
|
Note: you can’t overwrite this identifier, as it is used in several parts of the Socket.IO codebase.
Socket#handshake
This object contains some details about the handshake that happens at the beginning of the Socket.IO session.
|
Example:
|
Socket#rooms
This is a reference to the rooms the Socket is currently in.
|
Additional attributes
As long as you do not overwrite any existing attribute, you can attach any attribute to the Socket instance and use it later:
|
Events
On the server-side, the Socket instance emits two special events:
disconnect
This event is fired by the Socket instance upon disconnection.
|
Here is the list of possible reasons:
Reason | Description |
---|---|
server namespace disconnect |
The socket was forcefully disconnected with socket.disconnect() |
client namespace disconnect |
The client has manually disconnected the socket using socket.disconnect() |
server shutting down |
The server is, well, shutting down |
ping timeout |
The client did not send a PONG packet in the pingTimeout delay |
transport close |
The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G) |
transport error |
The connection has encountered an error |
disconnecting
This event is similar to disconnect
but is fired a bit earlier, when the Socket#rooms set is not empty yet
|
Note: those events, along with connect
, connect_error
, newListener
and removeListener
, are special events that shouldn’t be used in your application:
|
Complete API
The complete API exposed by the Socket instance can be found here.
© 2014–2021 Automattic
Licensed under the MIT License.
https://socket.io/docs/v3/server-socket-instance