ServerConnection

ServerConnection

(private) new ServerConnection()

Source:
Properties:
Name Type Description
id string

Unique connection id

Server connection class. Constructor of this class is not publicly accessible. When you listen Server.Event.CONNECTION or Server.Event.HANDSHAKE, an instance of ServerConnection will be emitted.

Extends

  • EventEmitterExtra

Members

(static, readonly) ErrorCode :string

Source:
Properties:
Name Type Description
MESSAGE_TIMEOUT string

This error can be seen in rejection of serverConnection.send() method.

MESSAGE_REJECTED string

This error can be seen in rejection of serverConnection.send() method, which again indicates that server is explicitly rejected the message.

MESSAGE_NOT_RESPONDED string

When the response of a message failed to send to client, this error will be emitted in ServerConnection.Event.ERROR event.

INVALID_JSON string

Indicates an error while json parsing/stringify.

HANDSHAKE_ENDED string

This error can be thrown in serverConnection.setId(). Connection id cannot be set after handshake.

ID_CONFLICT string

This error can be seen while using serverConnection.setId(). If there is already connection with that id, this error will be thrown.

DISCONNECTED string

This error indicates client is disconnected.

WEBSOCKET_ERROR string

This error is for native websocket errors.

PING_ERROR string

This error can be seen in rejection of serverConnection.ping() method.

Type:
  • string

(static, readonly) Event :string

Source:
Properties:
Name Type Description
ERROR string

_error

DISCONNECTED string

_close

Type:
  • string

(static, readonly) State :string

Source:
Properties:
Name Type Description
AWAITING_HANDSHAKE string

awaitingHandshake Connection is open but handshake is not completed yet.

CONNECTED string

connected Connection is open and handshake resolved.

DISCONNECTED string

disconnected There is no open connection.

Type:
  • string

Methods

close(codeopt, reasonopt, delayopt) → {Promise}

Source:

Gracefully closes the client connection.

Parameters:
Name Type Attributes Description
code number <optional>
reason string <optional>
delay number <optional>
Returns:
Type
Promise

getRooms() → {Array.<string>}

Source:

Gets the joined room names.

Returns:
Type
Array.<string>

joinRoom(roomName)

Source:

Joins the connection into provided room. If there is no room, it will be created automatically.

Parameters:
Name Type Description
roomName string

leaveRoom(roomName)

Source:

Leaves the connection from provided room.

Parameters:
Name Type Description
roomName string

ping() → {Promise}

Source:

Pings the client. If there is no respose, closes the connection.

Returns:
Type
Promise

send(name, payloadopt, timoutopt) → {Promise.<any>}

Source:

Sends a message to client with awaiting its response. This method returns a promise which resolves the payload parameter will be passed into message.resolve(...) in client-side.

If client rejects the message with message.reject(...), this promise will be rejected with ServerConnection.ErrorCode.MESSAGE_REJECTED. You can access the original error object with err.payload.

Rejections:

  • ServerConnection.ErrorCode.INVALID_JSON: Could not stringify the message payload. Probably circular json.
  • ServerConnection.ErrorCode.MESSAGE_REJECTED: Message is explicitly rejected by the client.
  • ServerConnection.ErrorCode.MESSAGE_TIMEOUT: Message response did not arrived, timeout exceeded.
  • ServerConnection.ErrorCode.DISCONNECTED: Client is not connected (& handshake resolved) or connection is closing
  • ServerConnection.ErrorCode.WEBSOCKET_ERROR: Native websocket error
Example
connection
  .send('hello', {optional: 'payload'})
  .then((data) => {
    // Message is resolved by client
  })
  .catch((err) => {
    // Could not send message
    // or
    // Client rejected the message!
  });
Parameters:
Name Type Attributes Description
name string
payload any <optional>
timout number <optional>
Returns:
Type
Promise.<any>

sendWithoutResponse(name, payloadopt) → {Promise}

Source:

Sends a message to client without waiting its response. This method returns a promise that resolves with nothing if the message is successfully sent.

Rejections:

  • ServerConnection.ErrorCode.INVALID_JSON: Could not stringify the message payload. Probably circular json.
  • ServerConnection.ErrorCode.DISCONNECTED: Client is not connected (& handshake resolved) or connection is closing
  • ServerConnection.ErrorCode.WEBSOCKET_ERROR: Native websocket error
Example
connection
  .sendWithoutResponse('hello', {optional: 'payload'})
  .then(() => {
    // Message sent successfully
  })
  .catch((err) => {
    // Message could not be sent to client
  })
Parameters:
Name Type Attributes Description
name string
payload any <optional>
Returns:
Type
Promise

setId(newId)

Source:

Changes connection's id, it's random by default. This method is helpful if you already have custom identification for your clients. You must do this before handshake resolved. If handshake is already resolved or there is conflict, this method will throw error.

Throws:

  • ServerConnection.ErrorCode.HANDSHAKE_ENDED: Id could not be changed after handshake
  • ServerConnection.ErrorCode.ID_CONFLICT: There is alrady another connection with provided id.
Example
server.on(Server.Event.HANDSHAKE, (connection, handshake) => {
  // Assuming client's `options.handshake.payload` is something like `{authToken: '...'}`

  // Imaginary db
  db.find(handshake.payload.authToken, (record) => {
    if (!record) return handshake.reject(new Error('Invalid auth token'));
    connection.setId(record.id);
    handshake.resolve(record);
  });
});
Parameters:
Name Type Description
newId string

New connection id