I am trying to send a message to a client connected through the socket by using **AWS.ApiGatewayManagementApi() postToConnection**
method. It's working fine in local, but not working in ec2. The only difference is VPC. Is that would be a problem?
var AWS = require('aws-sdk');
var awsGW = new AWS.ApiGatewayManagementApi({
endpoint: "https://endpoint",
accessKeyId: "accessKeyId",
secretAccessKey: "secretAccessKey",
region: "region"
});
awsGW.postToConnection({
ConnectionId: 'ConnectionId',
Data: '{"key1":"msg1","key2":"msg2"}'
}, (err, success) => {
if (err) {
console.log('Socket error', err);
} else {
}
});
ERROR:
{ ForbiddenException: Forbidden
at Object.extractError (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.extractError (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /home/ubuntu/git/backend/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:685:12)
message: 'Forbidden',
code: 'ForbiddenException',
time: 2019-06-10T07:40:02.214Z,
requestId: 'f4caef03-8b52-11e9-9cc3-91bfe40b8eb0',
statusCode: 403,
retryable: false,
retryDelay: 37.0834357877396 }
Package apigatewaymanagementapi provides the client and types for making API requests to AmazonApiGatewayManagementApi. The Amazon API Gateway Management API allows you to directly manage runtime aspects of your deployed APIs.
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway . In the APIs navigation pane, choose the API you want to deploy. In the Resources navigation pane, choose Actions. From the Actions drop-down menu, choose Deploy API.
I ran into a similar problem and it was because I was using the wrong endpoint. I was generating it from the event
object but on a normal API request, so I was getting an endpoint for a different service (the normal API, and not the websocket).
If your websockets service is deployed to a URL like wss://abcd
then your endpoint should be https://abcd
.
In my case, there was just an error/undefined value in the endpoint I was using to instantiate the
new AWS.ApiGatewayManagementApi({
endpoint,
region,
})
Note: endpoint can provided in 2 forms:
wss.my-super-website-with-websocket.com/v1
(where v1 is the configured base path mapping)https://********.execute-api.us-east-5.amazonaws.com/prod
where prod is the stage configured in API Gateway.More importantly, for your debugging purposes, if you get desperate, I found out that you can quite easily log the request made by aws-sdk
to make sure you are making the correct request (using the aws cli to try the request is also a good idea):
node_modules\aws-sdk\lib\event_listeners.js
function executeSend() {
var http = AWS.HttpClient.getInstance();
var httpOptions = resp.request.service.config.httpOptions || {};
try {
console.log('event listener execute send', resp.request.httpRequest); // only this line is added
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With