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

Allow WebSocket responses to be sent by handler responses #1008

Closed
kevbot-git opened this issue Jun 7, 2020 · 6 comments
Closed

Allow WebSocket responses to be sent by handler responses #1008

kevbot-git opened this issue Jun 7, 2020 · 6 comments
Labels

Comments

@kevbot-git
Copy link

Feature Request

It is currently possible to use ApiGatewayManagementApi.postToConnection to respond to an incoming WebSocket message as detailed in the
readme.
However, it would be great to have serverless-offline act in the same way as API Gateway's WebSocket APIs are able to, meaning that the body in the return value of the handler is sent back to the connected client as a WebSocket message.

Sample Code

  • file: serverless.yml
service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs12.x
  stage: dev

custom:
  serverless-offline:
    websocketPort: ${self:custom.websocketPort}

functions:
  ws-custom:
    handler: handler.wsCustom
    events:
      - websocket:
          route: custom
          routeResponseSelectionExpression: $default
  • file: handler.js
'use strict'

module.exports.wsCustom = async (event) => {
  const body = JSON.parse(event.body);
  const payload = { message: `Hello, ${body.name}!` };

  return {
    statusCode: 200,
    body: JSON.stringify(payload)
  }
}

Expected behavior/code

If I were to deploy to AWS and run the following code in browser:

ws = new WebSocket('wss://<appid>.execute-api.ap-southeast-2.amazonaws.com/dev');
ws.onmessage = message => { console.log('message', message); };
ws.onerror = error => { console.log('error', error); };
ws.send(JSON.stringify({ action: '', name: 'kevbot-git' }));

...I would expect to see a WebSocket message come back straight away in response. However, when running locally:

ws = new WebSocket('http://localhost:3001');
ws.onmessage = message => { console.log('message', message); };
ws.onerror = error => { console.log('error', error); };
ws.send(JSON.stringify({ action: '', name: 'kevbot-git' }));

...the WebSocket connection is successful and any logs will show in the serverless console, but no response WebSocket message will be sent.

Additional context/Screenshots

@kevbot-git
Copy link
Author

Another way to repro this issue is simply to run the project's WebSocket example. When I run it, the return values do not get returned to the client in any form.

@kevbot-git
Copy link
Author

Another interesting point is that if I put code in the handler that throws an unhandled error, the WebSocket client does receive a message with the error message, ‘Internal server error’. At least there is some form of two-way communication working!

@mbarlocker
Copy link

+1

@dherault
Copy link
Owner

This should be an easy fix. @frozenbonito what do you think?

@frozenbonito
Copy link
Contributor

// let result
try {
/* result = */ await lambdaFunction.runHandler()
// TODO what to do with "result"?
} catch (err) {
console.log(err)
sendError(err)
}

The result need to be handled properly.
I think we'll need to do some research into the actual behavior of API Gateway.

kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Aug 19, 2020
kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Aug 19, 2020
kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Aug 19, 2020
kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Aug 20, 2020
kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Aug 20, 2020
kevbot-git added a commit to kevbot-git/serverless-offline that referenced this issue Mar 25, 2021
@dnalborczyk dnalborczyk reopened this Nov 2, 2022
@dnalborczyk dnalborczyk added the bug label Nov 2, 2022
@dnalborczyk
Copy link
Collaborator

fixed with: #1301

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

Successfully merging a pull request may close this issue.

5 participants