Amazon aws websocket api - forbidden on message send
Asked Answered
D

7

7

I'm trying to implement the websocket api for my project, so I was looking at this tutorial https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/

I tried to deploy the simple chat app they linked https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:729047367331:applications~simple-websockets-chat-app

I was able to successfully connect using wscat -c wss://url, but when I try to send a message I get the following error (there are actual id's I'm just not sure where they're generated from so I left them hidden)

{"message": "Forbidden", "connectionId":"sample_id", "requestId":"sample_id"}

I'm not really sure what's wrong as this is supposed to be a completed version (I did successfully deploy and connect to it?) and I would love some assistance

Deeprooted answered 8/5, 2019 at 14:55 Comment(5)
Did you find any solution for this? I'm stuck in same problemAxes
Unfortunately I haven't, please let me know if you doDeeprooted
Same here, as a workaround I'm actually using a 3th party websocket server.Axes
Also, this has been discussed hereAxes
if you configured the apis disconnect, connect and default by hand with the lambda integration, you have to deploy the API again. Otherwise it will have no effect.Refresher
M
5

Before creating any working route you must have been configured the $default route on API Gateway.

Matronly answered 25/2, 2021 at 10:30 Comment(0)
S
3

This worked for me needed to stringify the object and pass and action and data

var test = { action: "sendmessage", data: "hello world" };

ws.on("open", function open() {
  ws.send(JSON.stringify(test));
});
Switchman answered 9/11, 2020 at 22:33 Comment(2)
Yes, this is a solution, provided that sendmessage is the name of the RouteKey. You can ascertain this by checking API Gateway > Routes. Attention not to misspell it sendMessagePhytobiology
i am using amazon-connect, in that case do i need to create the route? I am sending this on socket -> {"topic":"aws/subscribe","content":{"topics":["aws/chat"]}} .. by this agent portal gets the notification for new chat.. how can i send message to agent now?Monteverdi
M
2

Once connected, try

{"message" : "sendMessage", "data" :" hi"}
Montage answered 19/5, 2020 at 17:18 Comment(1)
i am using amazon-connect, in that case do i need to create the route? I am sending this on socket -> {"topic":"aws/subscribe","content":{"topics":["aws/chat"]}} .. by this agent portal gets the notification for new chat.. how can i send message to agent now?Monteverdi
P
2

After fighting with this for a couple of hours, there are a few things that you can do:

  1. (recommended) create a custom route in your websocket api which will be where you are sending your messages to via the websocket connection. Then add a lambda integration (or other backend integration of your choice). In the send command you send the name of the route as the "action" in a JSON serialized object.
ws.send(JSON.stringify({ action: [customRoute], message: "hello" })

Note: you can send as many JSON fields as you want, just be sure to include "action"!

  1. Add a lambda integration (or other integration of your choice) to the $default route in the apigateway. All requests that are not action "Connect" or "Disconnect" go there and since you do not have a backend integration for that route AWS kindly tells you "Forbidden" like route missing in apigateway REST apis.

In both cases, after making the changes you need to Deploy the api (under the Actions tab) to have these changes take effect. All of this frustration could be solved by better AWS documentation...

Prothallus answered 6/4, 2022 at 23:22 Comment(0)
P
2

The message you are sending should be in JSON format. If it is JavaScript based, then you can use JSON.Stringfy( { action: "", cutomProp: "any values"}).

If anyone interesting with more clear explanation, this video will helpful. https://youtu.be/EZrXBnca4KY

Preterit answered 29/3 at 12:13 Comment(0)
K
0

This is what I am doing but still got the same

{"message": "Forbidden", "connectionId":"sample_id", "requestId":"sample_id"}

var test = { action: "sendmessage", data: "hello world" };
        connection.onopen = () => {
            connection.send(JSON.stringify(test));
            console.log("connection open?????");
        };
        connection.onerror = error => {
            console.log(`WebSocket error: ${error}`);
        };
        connection.onmessage = e => {
            console.log("On message come here?????")
            console.log("EEEE", JSON.stringify(e));
            console.log(e.data);
        };
Karleenkarlen answered 24/11, 2020 at 10:37 Comment(0)
A
-2

thanks to solution provided by @prachiSingh, was able to send a sample message by tweaking it a bit.... {"message" : "sendmessage", "data" :" hello"}. Basically using all lowercase for sendmessage.

Acme answered 21/9, 2020 at 16:53 Comment(2)
Please decide whether this is meant to be a "Thanks" (in that case please delete the post, as it is not an answer) or an answer (in that case delete the "thanks" and make the helpfully answering part more obvious).Abroms
Please don't add "thanks" as answers. They don't actually provide an answer to the question, and can be perceived as noise by its future visitors. Once you earn enough reputation, you will gain privileges to upvote answers you like. This way future visitors of the question will see a higher vote count on that answer, and the answerer will also be rewarded with reputation points. See Why is voting important.Abroms

© 2022 - 2024 — McMap. All rights reserved.