API Gateway Web-socket route selection not working

Viewed 158

I have created a basic example APIGateway for websocket and integrated same lambda to all $connect $disconnect and a test route

Routes

Route selection expression being $request.body.action, I am trying to send a message to my test route. But it doesn't seems to be happening and its being routed to $default in every case.

I have tried using postman, writing a client Node Application using ws npm module and using wscat too.

Code using NodeJS and ws module

const WebSocket = require('ws');

const ws = new WebSocket('wss://2liojtynmju.execute-api.us-east-1.amazonaws.com/dev');

ws.on('open', () => {
    console.log('connected, sending message');
    ws.send(JSON.stringify({
        action: 'test'
    }));
});
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
    console.log('disconnected');
    process.exit();
});

Logs in cloudwatch looking like this (route key is still $default)

requestContext: {
    routeKey: '$default',
    messageId: 'XXXXXX',
    eventType: 'MESSAGE',
    extendedRequestId: 'XXXXXX=',
    requestTime: '23/Sep/2021:08:24:54 +0000',
    messageDirection: 'XX',
    stage: 'dev',
    connectedAt: XXXXXXXXXXXXX,
    requestTimeEpoch: XXXXXXXX,
    identity: { sourceIp: 'XXXXXXXXXXXX' },
    requestId: 'XXXXXXXXXX=',
    domainName: 'XXXXXXXXXXXXXXXXXXXXX',
    connectionId: 'XXXXXXXXXXXXXXXX',
    apiId: 'XXXXXXXXXX'
  },
  body: '{"action":"test"}',
  isBase64Encoded: false
}

I am most certain that I am missing some key piece of information but couldn't find anything on internet so far.

1 Answers

I had this problem and finally solved it by changing the integration request type to LAMBDA PROXY.

The rest of your code looks like it should word.

Related