Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replicate AWS behavior of lambda exposed through API Gateway WebSockets #1188

Closed
wants to merge 1 commit into from

Conversation

VenomAV
Copy link

@VenomAV VenomAV commented Feb 26, 2021

Description

I changed the implementation of WebSocketClients._processEvent in order to use the result of lambda invocation.
If the result contains a body and the websocketClient is still OPEN, the body of the lambda result is sent to the client.

Motivation and Context

The current implementation doesn't behave like an AWS API Gateway with WebSockets. This change replicates the behavior exposed by AWS API Gateway.

How Has This Been Tested?

serverless.yml

service:
  name: test

plugins:
  - serverless-plugin-typescript
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x

functions:
  actionHandler:
    handler: src/echo-handler.handler
    events:
      - websocket:
          route: echo
          routeResponseSelectionExpression: $default

src/echo-handler.ts

import { APIGatewayProxyEvent, APIGatewayProxyResult, Callback, Context } from "aws-lambda"

export const handler = async (
    event: APIGatewayProxyEvent,
    context: Context,
    callback: Callback<APIGatewayProxyResult>,
): Promise<APIGatewayProxyResult> => {
    const body = JSON.parse(event.body)
    return {
        statusCode: 200,
        body: JSON.stringify({ message: `echo ${body.message}` }),
    }
}

Run serverless with serverless offline
Use wscat to interact with serverless:

$ wscat -c "ws://localhost:3001"
Connected (press CTRL+C to quit)
> {"action": "echo", "message": "ooooooooooooooooo"}
< {"message":"echo ooooooooooooooooo"}

Second to last line is the message sent to the API Gateway.
Last line is the lambda response which is missing in the current implementation

@kevbot-git
Copy link

Hey @VenomAV, I've had the same issue and opened #1064 a while ago... I'm wondering—would you see merit in me marking mine as duplicate, and you perhaps including the error response I added in that PR, in this one? We're keen to see it merged! 😄

@dherault
Copy link
Owner

1 yo PR with conflicts, please feel free to reopen.

@dherault dherault closed this Apr 13, 2022
@dnalborczyk
Copy link
Collaborator

superseded by: #1301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants