Class: Socket

Socket

new Socket(opts)

The client socket.
Parameters:
Name Type Description
opts Object The Socket and net.Socket options.
Properties
Name Type Attributes Default Description
reconnect Boolean <optional>
true Enable or disable the reconnection.
reconnectInterval Number <optional>
1000 The reconnection interval.
autoConnect Boolean <optional>
true Enable or disable the auto connection after instance the class.
useQueue Boolean <optional>
true Enable or disable the usage of an internal queue that will containt the emitted messages while the socket isn't connected. The enqueued messages will be sent as soon as the socket is connected.
queueSize Number <optional>
Infinity The max size of the queue. If the queue is full, new messages will replace old ones.
objectSerializer function <optional>
JSON.stringify Serializes an object into a binary buffer. This functions allows you to implement custom serialization protocols for the data or even use other known protocols like "Protocol Buffers" or "MessagePack".
objectDeserializer function <optional>
JSON.parse Deserializes a binary buffer into an object. This functions allows you to implement custom serialization protocols for the data type "object" or even use other known protocols like "Protocol Buffers" or "MessagePack".
Source:
Fires:

Extends

Members

id :String

A unique identifier. It will be set up asynchronously from the server in the event 'connect'.
Type:
  • String
Overrides:
Source:

Methods

connect()

Connect the socket. The socket will be connected automatically by default so this method is only useful when you use otps.autoConnect: false, in the constructor.
Inherited From:
Source:

destroy()

Close the socket.
Inherited From:
Source:

emit(event, data, paramopt) → {Boolean}

Emit an event.
Parameters:
Name Type Attributes Description
event String The event name.
data String | Number | Object | Buffer | Boolean The data to send.
param Object | function <optional>
The options or callback.
Properties
Name Type Attributes Default Description
sockets Array.<String> <optional>
[] The list of socket ids to send.
rooms Array.<String> <optional>
[] The list of rooms to send.
broadcast Boolean <optional>
false Send to all connected sockets.
Source:
Returns:
true if the entire data was flushed successfully. false if all or part of the data was queued in user memory. 'socket_drain' will be emitted when the buffer is again free.
Type
Boolean

end()

Send a FIN packet.
Inherited From:
Source:

getSerializerVersion() → {Number}

Get the version of the underlying serializer.
Inherited From:
Source:
Returns:
The serializer version.
Type
Number

join(room)

Join to a room.
Parameters:
Name Type Description
room String | Array.<String> The room name.
Source:

leave(room)

Leave a room.
Parameters:
Name Type Description
room String | Array.<String> The room name.
Source:

leaveAll()

Leave all rooms.
Source:

setKeepAlive(enableopt, initialDelayopt)

Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
Parameters:
Name Type Attributes Default Description
enable Boolean <optional>
false True to enable the TCP keep-alive, false to disable it.
initialDelay Number <optional>
0 Set the delay in milliseconds between the last data packet received and the first keepalive probe.
Inherited From:
Source:

setNoDelay(noDelayopt)

Disables the Nagle algorithm.
Parameters:
Name Type Attributes Default Description
noDelay Boolean <optional>
true True to disable de Nagle algorithm, false to enable it again.
Inherited From:
Source:

setTimeout(timeout)

Sets the socket to timeout after 'timeout' milliseconds of inactivity on the socket.
Parameters:
Name Type Description
timeout Number The timeout in milliseconds or 0 to disable it.
Inherited From:
Source:

stream(event, data, paramopt) → {stream.Writable}

Creates and returns a stream.Writable instance that can be used to stream binary data.
Parameters:
Name Type Attributes Description
event String The event name.
data String | Number | Object | Buffer | Boolean The data to send along with the stream.
param Object | function <optional>
The options or callback.
Properties
Name Type Attributes Default Description
sockets Array.<String> <optional>
[] The list of socket ids to send.
rooms Array.<String> <optional>
[] The list of rooms to send.
broadcast Boolean <optional>
false Send to all connected sockets.
Source:
Returns:
A stream.Writable instance.
Type
stream.Writable

Events

close

Close event from net.Socket.
Inherited From:
Source:

connect

The socket is connected and registered so Socket#id now contains the socket identification.
Source:

end

End event from net.Socket.
Inherited From:
Source:

error

Error event from net.Socket or Socket.
Inherited From:
Source:

reconnecting

The socket is trying to reconnect.
Inherited From:
Source:

socket_connect

Connected underlying net.Socket, all messages in queue will be sent and new messages will be sent directly.
Inherited From:
Source:

socket_drain

Emitted when the write buffer of the internal socket becomes empty. Can be used to throttle uploads.
Inherited From:
Source: