Matt Muller
Matt Muller

Reputation: 21

I have an error calling AWS API Gateway Websocket Route using wscat or via node.js

I create a websocket and then a custom route. Before publishing I need to select an integration for $disconnect and $default, for both I choose Mock (I have also tried default Lambda functions), this allows me to publish.

I then use wscat to call

wscat -c wss://t0p5b2xpm3.execute-api.us-east-1.amazonaws.com/prod

the socket connects successfully,then i try to call the route

{"action":"echo", "data":"test response body"}

and get the following error.

{"message": "Internal server error", "connectionId":"aDH97cQJoAMCI8Q=", "requestId":"aDIAhGE8oAMFoEg="}

anyone have any ideas please?

thanks,

Matt

Upvotes: 2

Views: 1282

Answers (2)

Hasoun GH
Hasoun GH

Reputation: 53

exports.handler = async (event) => {
  // TODO implement
  let time = "undefined";
  let state = "undefined";
  if (event.body !== null && event.body !== undefined) {
  let body = JSON.parse(event.body);
  if (body.time && body.state) 
      {
          time = body.time;
          state = body.state;
      }
  }
  const response = {
      statusCode: 200,
      body:   JSON.stringify({time:time, state: state}),
  };
  return response;
};

In case of API-Gateway you won't be able to modify the response as json as in Get and Post, so you can use this code to fetch time and state from the coming json object. Connect using wscat -c link and then send your json as {"time":"22:32","state":"LA"}

Try to put in $default to make sure that you are receiving the sending the right way.

Good luck!

Upvotes: 0

mnewton
mnewton

Reputation: 473

For a lambda function, you need to return a statusCode of 200 for $connect, $disconnect, $default, and custom routes. A trivial javascript example:

module.exports.handler = async (event) => {
  return {
    statusCode: 200,
  };
};

Upvotes: 1

Related Questions