- Socket
- socket.id
- socket.rooms
- socket.client
- socket.conn
- socket.request
- socket.handshake
- socket.use(fn)
- socket.send([…args][, ack])
- socket.emit(eventName[, …args][, ack])
- socket.on(eventName, callback)
- socket.once(eventName, listener)
- socket.removeListener(eventName, listener)
- socket.removeAllListeners([eventName])
- socket.eventNames()
- socket.join(room[, callback])
- socket.join(rooms[, callback])
- socket.leave(room[, callback])
- socket.to(room)
- socket.in(room)
- socket.compress(value)
- socket.disconnect(close)
- Flag: ‘broadcast’
- Flag: ‘volatile’
- Flag: ‘binary’
- Event: ‘disconnect’
- Event: ‘error’
- Event: ‘disconnecting’
Socket
A Socket
is the fundamental class for interacting with browser clients. A Socket
belongs to a certain Namespace
(by default /
) and uses an underlying Client
to communicate.
It should be noted the Socket
doesn’t relate directly to the actual underlying TCP/IP socket
and it is only the name of the class.
Within each Namespace
, you can also define arbitrary channels (called room
) that the Socket
can join and leave. That provides a convenient way to broadcast to a group of Socket
s (see Socket#to
below).
The Socket
class inherits from EventEmitter. The Socket
class overrides the emit
method, and does not modify any other EventEmitter
method. All methods documented here which also appear as EventEmitter
methods (apart from emit
) are implemented by EventEmitter
, and documentation for EventEmitter
applies.
socket.id
- (String)
A unique identifier for the session, that comes from the underlying Client
.
socket.rooms
- (Object)
A hash of strings identifying the rooms this client is in, indexed by room name.
|
socket.client
- (Client)
A reference to the underlying Client
object.
socket.conn
- (engine.Socket)
A reference to the underlying Client
transport connection (engine.io Socket
object). This allows access to the IO transport layer, which still (mostly) abstracts the actual TCP/IP socket.
socket.request
- (Request)
A getter proxy that returns the reference to the request
that originated the underlying engine.io Client
. Useful for accessing request headers such as Cookie
or User-Agent
.
|
socket.handshake
- (Object)
The handshake details:
|
Usage:
|
socket.use(fn)
fn
(Function)
Registers a middleware, which is a function that gets executed for every incoming Packet
and receives as parameter the packet and a function to optionally defer execution to the next registered middleware.
Errors passed to middleware callbacks are sent as special error
packets to clients.
|
socket.send([…args][, ack])
args
ack
(Function)- Returns
Socket
Sends a message
event. See socket.emit(eventName[, …args][, ack]).
socket.emit(eventName[, …args][, ack])
(overrides EventEmitter.emit
)
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 client’s answer.
|
socket.on(eventName, callback)
(inherited from EventEmitter
)
eventName
(String)callback
(Function)- Returns
Socket
Register a new handler for the given event.
|
socket.once(eventName, listener)
socket.removeListener(eventName, listener)
socket.removeAllListeners([eventName])
socket.eventNames()
Inherited from EventEmitter
(along with other methods not mentioned here). See the Node.js documentation for the events module.
socket.join(room[, callback])
room
(String)callback
(Function)- Returns
Socket
for chaining
Adds the client to the room
, and fires optionally a callback with err
signature (if any).
|
The mechanics of joining rooms are handled by the Adapter
that has been configured (see Server#adapter
above), defaulting to socket.io-adapter.
For your convenience, each socket automatically joins a room identified by its id (see Socket#id
). This makes it easy to broadcast messages to other sockets:
|
socket.join(rooms[, callback])
rooms
(Array)callback
(Function)- Returns
Socket
for chaining
Adds the client to the list of room, and fires optionally a callback with err
signature (if any).
|
socket.leave(room[, callback])
room
(String)callback
(Function)- Returns
Socket
for chaining
Removes the client from room
, and fires optionally a callback with err
signature (if any).
|
Rooms are left automatically upon disconnection.
socket.to(room)
room
(String)- Returns
Socket
for chaining
Sets a modifier for a subsequent event emission that the event will only be broadcasted to clients that have joined the given room
(the socket itself being excluded).
To emit to multiple rooms, you can call to
several times.
|
Note: acknowledgements are not supported when broadcasting.
socket.in(room)
Synonym of socket.to(room).
socket.compress(value)
value
(Boolean) whether to following packet will be compressed- Returns
Socket
for chaining
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.disconnect(close)
close
(Boolean) whether to close the underlying connection- Returns
Socket
Disconnects this client. If value of close is true
, closes the underlying connection. Otherwise, it just disconnects the namespace.
|
Flag: ‘broadcast’
Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the sender.
|
Flag: ‘volatile’
Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and is in the middle of a request-response cycle).
|
Flag: ‘binary’
Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be true
or false
.
|
Event: ‘disconnect’
reason
(String) the reason of the disconnection (either client or server-side)
Fired upon disconnection.
|
Possible reasons:
Reason | Side | Description |
---|---|---|
transport error | Server Side | Transport error |
server namespace disconnect | Server Side | Server performs a socket.disconnect() |
client namespace disconnect | Client Side | Got disconnect packet from client |
ping timeout | Client Side | Client stopped responding to pings in the allowed amount of time (per the pingTimeout config setting) |
transport close | Client Side | Client stopped sending data |
Event: ‘error’
error
(Object) error object
Fired when an error occurs.
|
Event: ‘disconnecting’
reason
(String) the reason of the disconnection (either client or server-side)
Fired when the client is going to be disconnected (but hasn’t left its rooms
yet).
|
These are reserved events (along with connect
, newListener
and removeListener
) which cannot be used as event names.