Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless WebSockets - No method found matching route @connections/* for http method POST

I'm using Serverless Framework to host my WebSocket, which has the typical $connect, $disconnect, $default, etc methods that updates my connections db:

case '$connect':
  await dynamoDb.put({
    TableName: process.env.CONNECTIONS_TABLE,
    Item: {
      connectionId,
      // Expire the connection an hour later. This is optional, but recommended.
      // You will have to decide how often to time out and/or refresh the ttl.
      ttl: parseInt((Date.now() / 1000) + 3600)
    }
  }).promise();

My WebSocket setup is:

WebSocket URL: wss://1111111111.execute-api.ap-southeast-2.amazonaws.com/dev/
Connection URL: https://1111111111.execute-api.ap-southeast-2.amazonaws.com/dev/@connections

My HTTP setup is:

Invoke API at: https://222222222.execute-api.ap-southeast-2.amazonaws.com/dev/

image

I have a broadcast function which I am using to send data to the connections, which I am invoking with:

sls invoke --function broadcast --data '{ \"body\": \"Hello from server\" }'

The source sends a message to each connection, as provided in the params of the request:

async function sendMessage(connectionId, body) {
  try {
    await apig.postToConnection({
      ConnectionId: connectionId,
      Data: body
    }).promise();
  } catch (err) {
    // Ignore if connection no longer exists
    if(err.statusCode !== 400 && err.statusCode !== 410) {
      throw err;
    }
  }
}

async function getAllConnections(ExclusiveStartKey) {
  const { Items, LastEvaluatedKey } = await dynamoDb.scan({
    TableName: process.env.CONNECTIONS_TABLE,
    AttributesToGet: [ 'connectionId' ],
    ExclusiveStartKey
  }).promise();

  const connections = Items.map(({ connectionId }) => connectionId);
  if(LastEvaluatedKey) {
    connections.push(...await getAllConnections(LastEvaluatedKey));
  }
  return connections;
}

module.exports.handler = async function(event, context) {
  const { body } = event;
  const connections = await getAllConnections();

  await Promise.all(
    connections.map(connectionId => sendMessage(connectionId, body))
  );
}

A connection can be established (I can connect to the WebSocket), however when I try to invoke this function I am receiving the error:

No method found matching route @connections/ZE4SDfSJSwMCJ4g%3D for http method POST.

The ZE4SDfSJSwMCJ4g is my connectionId, which exists in my database. I am unsure if this routing issue has to do with my HTTP API and my WebSocket API being pointed at different API Gateway URL?

I appreciate the help!

like image 759
CeraMix Avatar asked Dec 05 '25 10:12

CeraMix


1 Answers

Make sure that the endpoint in your API Gateway management configuration is the same as your ws endpoint.

const agma = new AWS.ApiGatewayManagementApi({
  apiVersion: AGMA_VERSION,
  endpoint: WS_ENDPOINT // 1111111111.execute-api.ap-southeast-2.amazonaws.com/dev
})
like image 178
solomonculaste Avatar answered Dec 06 '25 23:12

solomonculaste