Socket

server. Socket

Simple publish / subscribe wrapper built on top of the ws library.

An instance of server.Socket is automatically created per client when it connects (see server.Client#socket).

Important: In most cases, you should consider using a client.SharedState rather than directly using the Socket instance.

The Socket class contains two different WebSockets:

  • a socket configured with binaryType = 'blob' for JSON compatible data types (i.e. string, number, boolean, object, array and null).
  • a socket configured with binaryType= 'arraybuffer' for efficient streaming of binary data.

Members

sockets :server.Sockets

Description:
  • Reference to the @link{server.Sockets} instance.

    Allows for broadcasting from a given socket instance.

Source:

Reference to the @link{server.Sockets} instance.

Allows for broadcasting from a given socket instance.

Type:
Example
socket.sockets.broadcast('my-room', this, 'update-value', 1);

Methods

(channel, …args)

Description:
  • Dipatch an event to the listeners of the given channel.

Source:
Parameters:
Name Type Attributes Description
channel string

Channel name.

args * <repeatable>

Content of the message.

addListener(channel, callback)

Description:
  • Listen messages with JSON compatible data types on a given channel.

Source:
Parameters:
Name Type Description
channel string

Channel name.

callback function

Callback to execute when a message is received. Arguments of the callback function will match the arguments sent using the server.Socket#send method.

addToRoom(roomId)

Description:
  • Add the socket to a room

Source:
Parameters:
Name Type Description
roomId string

Id of the room.

kSocketTerminate()

Description:
  • Removes all listeners and immediately close the web socket.

    Is automatically called when socket is closed on the client side or when Server is stopped

Source:

removeAllListeners(channel)

Description:
  • Remove all listeners of messages with JSON compatible data types.

Source:
Parameters:
Name Type Default Description
channel string null

Channel name.

removeFromRoom(roomId)

Description:
  • Remove the socket from a room

Source:
Parameters:
Name Type Description
roomId string

Id of the room.

removeListener(channel, callback)

Description:
  • Remove a listener of messages with JSON compatible data types from a given channel.

Source:
Parameters:
Name Type Description
channel string

Channel name.

callback function

Callback to remove.

send(channel, …args)

Description:
  • Send messages with JSON compatible data types on a given channel.

Source:
Parameters:
Name Type Attributes Description
channel string

Channel name.

args * <repeatable>

Payload of the message. As many arguments as needed, of JSON compatible data types (i.e. string, number, boolean, object, array and null).