Module: websocket-promisifier

This module provides an API for a websocket connection. Services provided by one side can be called by the other through a promisified cross-network function (service) call, and use of a communication server as middleman so that clients that themselves cannot provide a network service can act as service providers through the middleman.

Receiving events from the communication partner(s) is possible by providing an event-message receiving service on the client's side.

ONE.core uses this module for its chum-sync module. The entire module can be used by apps using the one.core library on both sides to provide cross-client communication.

Source:

Methods

(static) createWebsocketPromisifier(connection) → {WebsocketPromisifierAPI}

Parameters:
Name Type Description
connection EncryptedConnectionInterface
Source:
Returns:

Returns a WebsocketPromisifierAPI object

Type: WebsocketPromisifierAPI

Type Definitions

WebsocketPromisifierAPI

The API assumes that all communication goes through a communication server that forwards messages between clients that usually could not connect directly to one another. Client code receives the API-object of this type when the other client has connected to the same communication server, so that an end-to-end connection exists with the comm-server in the middle as forwarding agent.

Use send() to send requests, use close() to end the connection. The final close status consisting of code and reason from the WebSocket's CloseEvent (see https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent) are available directly through the promise. If the CloseEvent was the result of calling close() as either a resolved promise if the code was 1000 (NORMAL), or a rejected promise with an Error object with code and reason in the Error's message string property.

Note that none of the API functions need to be bound (they don't use this).

Type:
  • object
Properties:
Name Type Description
promise Promise.<{code:number, reason:string}>

Promise tracking the websocket state

connId number

The ID of the underlying Connection

addService function
removeService function
clearServices function
connect function
send function

1st param: Service ID, rest: params for service function

close function

Initiate connection shutdown, results are in `promise. The optional close reason string must be no longer than 123 bytes (encoded in UTF-8). If it is longer, one.models' WebSocketPlugin close() function will shorten it automatically.

stats WebsocketStatistics

Read-only access to sent/received bytes and nr. of requests statistics

Source: