[AWS] WebSockets with API Gateway
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AWS] WebSockets with API Gateway相关的知识,希望对你有一定的参考价值。
User connects to API Gateway with some payload, Apigateway routing the request to different Lambda functions:
There are some special routes defined for websockets,
- `$connect`: When user connect to API Gateway thought websocket
- `$disconnet`:
- `$defualt`: default one, when there is no matching route
API Gateway WebSocket URL
WebSocket API provides two URLs: WebSocket URL and Connection URL.
WebSocket URL:
- Clinent use to connect to API
- Allows clients to send message and receive notifications
Connection URL:
- Send back message to client
- Lambda function use it to send message
- Requires a connection id to send a message to particular client
Connection id will be stored in DynamoDB table, a lambda function will read from it.
Connection URL supports the following operations:
- POST: to send a message to a client
- GET: to get the latest connection status
- DELETE: to disconnect a client from API
Here is an example of how to react to WebSocket events using Serverless Framework:
ConnectHandler:
handler: src/websocket/connect.handler
events:
- websocket:
route: $connect
DisconnectHandler:
handler: src/websocket/disconnect.handler
events:
- websocket:
route: $disconnect
Installing Tool
npm install wscat -g
wscat -c wss://52zeaf29d.exeute-api.eu-central-1.amazonaws.com/dev
以上是关于[AWS] WebSockets with API Gateway的主要内容,如果未能解决你的问题,请参考以下文章
无法通过 websockets 发布到 aws mqtt 代理
在 AWS Elastic Load Balancer 后面使用 Primus.io (websockets)
Spring with STOMP over SockJS 和 Tomcat 未升级到 Websockets
错误:INVALID_STATE_ERR: DOM Exception 11 with php websockets
我是不是需要在 DB 上进行会话集群以在 AWS/EC2 上使用 HAProxy 对 Jetty WebSockets 服务器进行负载平衡?