zoukankan      html  css  js  c++  java
  • [AWS] WebSockets with API Gateway

    Websocket is a bit different from HTTP, they work on top of a TCP connection.

    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
  • 相关阅读:
    Spring--之旅
    体验 Windows 系统 CVM
    基于 CentOS 搭建 WordPress 个人博客
    Codeforces Round #316 (Div. 2)
    BZOJ 1048 [HAOI2007]分割矩阵
    BZOJ 1047 [HAOI2007]理想的正方形
    BZOJ 1045 [HAOI2008] 糖果传递
    BZOJ 1042 [HAOI2008]硬币购物
    BZOJ 1030 [JSOI2007]文本生成器
    BZOJ 第二波刷题开始喽
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14782429.html
Copyright © 2011-2022 走看看