exploration into Rust and serverless websockets
$ npm ci && npx serverless deploy
You can use the wscat
command line utility to connect and communicate with your
serverless application.
$ npx wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/dev
This should open up an interactive repl with which you can communicate with the server
You can send messages to the server with a json payload containing an "action" field of "send" with an optional text "message" field
connected (press CTRL+C to quit)
> {"action":"send"}
< {"message":"🏓 pong"}
> {"action":"send", "message":"psst"}
< {"message":"psst"}
A typical websocket server requires an ability to speak a binary protocol over an upgraded http protocol connection. By its nature, that requires the operational capability to maintain a long lived persistant connection with many connected clients.
A server with a sturdy security posture should also provide a means of encrypting information passed across network connections. Secure websocket connections require additional handshake procedures which requires additional binary protocol extensions that you are responsible for.
API Gateway replaces the need for you to write and operator traditional websocket servers. API Gateway exposing a tls (wss) websocket endpoint and manages persistent connections for you freeing you up to focus application specific details.
Your application need only implement functions to be invoked to specific lifecycle events called "routes". A few special routes are $connect
$disconnect
and $default
which represent a new client connecting, an existing client disconnecting, and an unmapped request route respectively. You are not responsible for maintaining a network connections but you are responsible
for maintaining connection identifiers. Managed serverless data stores like Dynamo DB make storing this trivially simple.
You can also route based on a request pattern. By default, with Serverless framework, it's expected connected clients send the server JSON payloads containing an "action" field which a handler gets routed to based on its value. This example application routes on an action called "send".
Doug Tangren (softprops) 2019