@ws

Define WebSocket endpoint and Lambda handler functions.

Example

This app.arc file defines both HTTP and WebSocket endpoints:

arc
@app
myapp

@ws
json
{
  "architect": {
    "app": "myapp"
    "ws": {}
  },
  "start": "npx sandbox",
  "dependencies": {
    "@architect/architect": "latest"
  }
}
yaml
---
app: testapp

ws: ~

Running arc create generates the following functions:

/
├── ws
│   ├── connect
│   ├── default
│   ├── disconnect
├── app.arc
└── package.json

Each function responds to WebSocket events from a client.

In the payload delivered to the function there is a connectionId that uniquely identifies a client. This connectionId can be used to send messages to the correct client.

Functions

The functions created by the @ws pragma handle events from a WebSocket client.

  • Connect - This function is invoked when a WebSocket client connects to the application
  • Default - This function is invoked when a WebSocket client sends a message to the application
  • Disconnect - This function is invoked when a WebSocket client disconnects from your application

Function Payload

Argument Description
req The WebSocket request payload
req.requestContext.connectionId An id that uniquely identifies a client

Send Messages

To publish a message to a WebSocket client you can use arc’s runtime library @architect/functionsws.send method. You can call this method from any of your application’s functions.

Docs: Node.js - Ruby - Python