amazon-web-serviceswebsocketrxjsgateway

rxjs/webSocket client connects to AWS api gateway websocket server with a specific routeKey


I've implemented a websocket service using AWS API gateway and tested it successfully with wscat:

$ wscat -c wss://<endpoint>.execute-api.<region>.amazonaws.com/dev/
Connected (press CTRL+C to quit)
> {"action": "testRouteKey", "msg": "hello from wscat"}
< {"msg":"Got 'hello from wscat'"}

The service has a custom routeKey "testRouteKey" which could be accessed by wscat as shown above.

How can I implement the above wscat activity using https://rxjs.dev/api/webSocket ? In my attempt, I could access the $default routeKey using this code:

(new WebSocketSubject("wss://<endpoint>.execute-api.<region>.amazonaws.com/dev/"))
.next(JSON.stringify({
    "action": "testRouteKey",
    "msg": "hello from rxjs"
}));

CloudWatch shown that the rxjs client accessed the $default routeKey and the complete JSON payload appeared in requestContext.body. How to specify the custom routeKey "testRouteKey"?


Solution

  • I forgot that the source code of wscat is available and can be used as a reference https://github.com/websockets/wscat/blob/master/bin/wscat

    In fact, aws api gateway expects the payload format just as used in my wscat test. The mistake in my js code was WebSocketSubject.next expects an object not JSON string.