- Socket
- socket.id
- socket.connected
- socket.disconnected
- socket.open()
- socket.connect()
- socket.send([…args][, ack])
- socket.emit(eventName[, …args][, ack])
- socket.on(eventName, callback)
- socket.compress(value)
- socket.binary(value)
- socket.close()
- socket.disconnect()
- Events
- Event: ‘connect’
- Event: ‘disconnect’
- Event: ‘error’
- Event: ‘connect_error’
- Event: ‘connect_timeout’
- Event: ‘reconnect’
- Event: ‘reconnect_attempt’
- Event: ‘reconnecting’
- Event: ‘reconnect_error’
- Event: ‘reconnect_failed’
- Event: ‘ping’
- Event: ‘pong’
Socket
A Socket
is the fundamental class for interacting with the server. A Socket
belongs to a certain Namespace (by default /
) and uses an underlying Manager to communicate.
A Socket
is basically an EventEmitter which sends events to — and receive events from — the server over the network.
|
socket.id
- (String)
An unique identifier for the socket session. Set after the connect
event is triggered, and updated after the reconnect
event.
|
socket.connected
- (Boolean)
Whether or not the socket is connected to the server.
|
socket.disconnected
- (Boolean)
Whether or not the socket is disconnected from the server.
|
socket.open()
- Returns
Socket
Manually opens the socket.
|
It can also be used to manually reconnect:
|
socket.connect()
Synonym of socket.open().
socket.send([…args][, ack])
args
ack
(Function)- Returns
Socket
Sends a message
event. See socket.emit(eventName[, …args][, ack]).
socket.emit(eventName[, …args][, ack])
eventName
(String)args
ack
(Function)- Returns
Socket
Emits an event to the socket identified by the string name. Any other parameters can be included. All serializable datastructures are supported, including Buffer
.
|
The ack
argument is optional and will be called with the server answer.
|
socket.on(eventName, callback)
eventName
(String)callback
(Function)- Returns
Socket
Register a new handler for the given event.
|
The socket actually inherits every method of the Emitter class, like hasListeners
, once
or off
(to remove an event listener).
socket.compress(value)
value
(Boolean)- Returns
Socket
Sets a modifier for a subsequent event emission that the event data will only be compressed if the value is true
. Defaults to true
when you don’t call the method.
|
socket.binary(value)
Specifies whether the emitted data contains binary. Increases performance when specified. Can be true
or false
.
|
socket.close()
- Returns
Socket
Disconnects the socket manually.
socket.disconnect()
Synonym of socket.close().
Events
The Socket
instance emits all events sent by its underlying Manager, which are related to the state of the connection to the server.
It also emits events related to the state of the connection to the Namespace:
connect
,disconnect
error
.
Event: ‘connect’
Fired upon connection to the Namespace (including a successful reconnection).
|
Event: ‘disconnect’
reason
(String)
Fired upon disconnection. The list of possible disconnection reasons:
Reason | Description |
---|---|
io server disconnect | The server has forcefully disconnected the socket with socket.disconnect() |
io client disconnect | The socket was manually disconnected using socket.disconnect() |
ping timeout | The server did not respond in the pingTimeout range |
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 (example: the server was killed during a HTTP long-polling cycle) |
In all cases but the first (disconnection by the server), the client will wait for a small random delay and then reconnect.
|
Event: ‘error’
error
(Object) error object
Fired when an error occurs.
|
Event: ‘connect_error’
error
(Object) error object
Fired upon a connection error.
|
Event: ‘connect_timeout’
Fired upon a connection timeout.
|
Event: ‘reconnect’
attempt
(Number) reconnection attempt number
Fired upon a successful reconnection.
|
Event: ‘reconnect_attempt’
attempt
(Number) reconnection attempt number
Fired upon an attempt to reconnect.
|
Event: ‘reconnecting’
attempt
(Number) reconnection attempt number
Fired upon an attempt to reconnect.
|
Event: ‘reconnect_error’
error
(Object) error object
Fired upon a reconnection attempt error.
|
Event: ‘reconnect_failed’
Fired when the client couldn’t reconnect within reconnectionAttempts
.
|
Event: ‘ping’
Fired when a ping is sent to the server.
|
Event: ‘pong’
ms
(Number) number of ms elapsed sinceping
packet (i.e.: latency).
Fired when a pong is received from the server.
|
Caught a mistake? Edit this page on GitHub