Websocket Trigger

Summary

Register a WebSocket route. Events are produced when a WebSocket connection is established, WebSocket messages are received and when a connection is closed. Message can be responded to with an response event.

A common pattern is routing the req and close ports to a connection manager node. The connection manager can then dispatch messages as required. In the example below the manager supports broadcasting a message to all connected clients.

Ports

When a WebSocket connection is established by a client an event is routed to the req port.

When a message is received from a connected WebSocket client an event is routed to the message port.

When a WebSocket connection is closed an event is routed to the close port.

Input/Output Format

All produced events are objects. All events have the following properties:

  • peer,
  • sessionId,
  • method,
  • params,
  • query, and
  • headers.

Events routed to the message port also has the property message.

The peer property holds a string describing the IP address of the client. (May be a list of IPs if the client is connected through proxies.)

The sessionId property holds a string that is unique for the current connection.

The method property holds a string corresponding to the HTTP method used to connect.

The params property holds an object as described here.

The query property holds an object as described here.

The headers property holds an object as described here.

The message property holds contains the sent WebSocket message.

Events

This functions listens for an event local response event when a req or message event is routed. When a response event is received, a message is sent to the client the originally triggered the orgininal req or message event.

The argument for the response event is expected to be an object of the following form {body, end}. end is optional. If present and it is truthy, the connection to the client is closed. body may be any value.

Other

See us use the WebSocket trigger here.