Websocket Related Functions
Hub
Low-level websocket handler for automaton. Before you use this, make sure you actually want to and not the more convenient pubsub implemented on top of this.
.register(type, fn)
Registers a handler for the given message type.
Parameters
Name | Type | Description |
---|---|---|
type | String | message type |
fn | function | callback that will receive the message payload |
Return Value
Type: () => voidunregister function
.send(type, payload)
Sends a message via websocket.
Parameters
Name | Type | Description |
---|---|---|
type | String | message type |
payload | object | message payload |
Return Value
Type: numbermessage id
.request(type, payload)
Initiate a request/response over websocket. The server has to react to the message received by sending a Response message referencing the message id of the message.
Parameters
Name | Type | Description |
---|---|---|
type | String | message type |
payload | ------ | message payload |
Return Value
Type: Promise<unknown>No description
.init(cid)
Initializes the Hub with the prepared connection id for the client.
Parameters
Name | Type | Description |
---|---|---|
cid | String | connection id |
Return Value
Type: Promise<String>resolves with the connection id after the websocket connection is established.
.getConnectionId()
Returns the current connection id.
Return Value
Type: Stringconnection id
publish(topic, message)
Publishes the given message payload for the given topic
Parameters
Name | Type | Description |
---|---|---|
topic | String | topic name |
message | object | message payload |
subscribeToTopic(topic, handler)
Subscribes the given message handler to the given topic. Returns an unsubscribe function.
Parameters
Name | Type | Description |
---|---|---|
topic | String | topic name |
handler | Function | message handler function. Receives the (filtered) messages for the given topic. |