Intermediate Topics¶
IDs¶
As of Socket.IO 4, the whole connection has a global ID and all namespaces (sockets) have theirs too.
Connection ID¶
The connection's ID can be accessed through the SocketManager
's Handshake
property:
Example
Socket ID¶
Per-socket ID, witch is received when the namespace (socket) is connected, can either be accessed through the connect event as a parameter, or later through the socket instance:
Example
ConnectResponse
's sid
and Socket
's Id
are the same value.
ConnectResponse
can be found in the Best.SocketIO.Events namespace.
Special Events¶
connect
and error
events are special as their type parameters are already defined. connect
emits a ConnectResponse
object that has only one field: sid
Example
Specifying a different type parameter for connect
will produce an error.
On the other hand, while error
has a predefined Error
type with a message
property, a new, custom error type can be created and used.
Example
For example a server-side middleware might want to send additional data other than just a plain text:
Server | |
---|---|
Namespaces¶
Using namespaces, logic can be separated both on the client and the server. SocketManager
's Socket
property is bound to the default, root ('/') namespace. Every subscription and event sent through this is sent to the root namespace.
Using the GetSocket
function, a new, custom namespace can be created and connected to. Note, that it must be available on the server too. For documentation about the server, check out the Socket.IO Namespaces topic.
Example
Create custom namespace on the server | |
---|---|
Connect to a custom namespace | |
---|---|
Rooms¶
Rooms are completely server-side features, no client support required!
Reconnection¶
SocketManager will try to reconnect if all of the following preconditions are met: 1. there's a timeout or the transport unintentionally disconnects from the server, 1. the SocketOptions' Reconnection
is true
.
During reconnection, "disconnect"
, "reconnect_attempt"
, "reconnecting"
events are emitted. These are local events, generated locally. Non-volatile events emitted to the server are buffered up and sent to the server once it could reconnect.
It's good practice to use volatile events for non important events and/or pause event sending during reconnect.
SocketOptions has numerous properties to alter and fine-tune reconnection logic.