Aws-sdk-js: ApiGatewayManagementApi is missing deleteConnection

Created on 19 Feb 2019  Â·  10Comments  Â·  Source: aws/aws-sdk-js

AWS.ApiGatewayManagementApi currently has method postToConnection which can be used to send messages to an active WebSocket connection in API Gateway for WebSocket. However, the other action that needs to be triggerable is to close a connection. This happens by sending a DELETE method request to the same URL the POST request is being sent to:

~/.local/bin/awscurl -X DELETE https://p6k5ovg070.execute-api.us-east-1.amazonaws.com/live/%40connections/VWi0WdQt0kwCFHg=
{ "nSent" : "1", "nReceived" : "1", "connectionCreateEpochMillis" : "1550587931880", "lastActiveEpochMillis" : "1550587931880" }

The name of this method should probably be deleteConnection, but obviously disconnectConnection and closeConnection are also possibilities.

feature-request

Most helpful comment

I came here through several Google search and found out that we actually don't have it in the sdk.
So I've tried to "patch", and it works fine, hope the Aws team will add this api soon!

1) Create file patch.js

require('aws-sdk/lib/node_loader');
var AWS = require('aws-sdk/lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;

apiLoader.services['apigatewaymanagementapi'] = {};
AWS.ApiGatewayManagementApi = Service.defineService('apigatewaymanagementapi', ['2018-11-29']);
Object.defineProperty(apiLoader.services['apigatewaymanagementapi'], '2018-11-29', {
    get: function get() {
        var model = {
            "metadata": {
                "apiVersion": "2018-11-29",
                "endpointPrefix": "execute-api",
                "signingName": "execute-api",
                "serviceFullName": "AmazonApiGatewayManagementApi",
                "serviceId": "ApiGatewayManagementApi",
                "protocol": "rest-json",
                "jsonVersion": "1.1",
                "uid": "apigatewaymanagementapi-2018-11-29",
                "signatureVersion": "v4"
            },
            "operations": {
                "PostToConnection": {
                    "http": {
                        "requestUri": "/@connections/{connectionId}",
                        "responseCode": 200
                    },
                    "input": {
                        "type": "structure",
                        "members": {
                            "Data": {
                                "type": "blob"
                            },
                            "ConnectionId": {
                                "location": "uri",
                                "locationName": "connectionId"
                            }
                        },
                        "required": ["ConnectionId", "Data"],
                        "payload": "Data"
                    }
                },
                "DeleteConnection": {
                    "http": {
                        "requestUri": "/@connections/{connectionId}",
                        "responseCode": 200,
                        "method": "DELETE"
                    },
                    "input": {
                        "type": "structure",
                        "members": {
                            "ConnectionId": {
                                "location": "uri",
                                "locationName": "connectionId"
                            }
                        },
                        "required": [
                            "ConnectionId"
                        ]
                    }
                }
            },
            "shapes": {}
        }
        model.paginators = {
            "pagination": {}
        }
        return model;
    },
    enumerable: true,
    configurable: true
});

module.exports = AWS.ApiGatewayManagementApi;

2) Apply the patch

const AWS = require('aws-sdk');
require('./patch.js');

3) Use it!

  const apigwManagementApi = new AWS.ApiGatewayManagementApi({
    apiVersion: '2018-11-29',
    endpoint: '---ehh I dont want to show it here---'
  });
  drop = async (connectionId) => {
    await apigwManagementApi.deleteConnection({ ConnectionId: connectionId }).promise();
  }

All 10 comments

Similarly I think an API like getConnectionStatus would be nice to have as well.

Is it possible to contribute to the SDK to provide these abilities?

@nakedible-p

Thanks for submitting this feature request. We'll review for prioritization.

This is important. Currently, it is not possible to "kick" a connection, you just have to send them a disconnect message, and they need to choose to accept it. Clients could theoretically be modified to never close the connection, and incur large bills on the AWS Account owner...

@rodolphito —

We force close connections by issuing a HTTP DELETE request on /@connections/:id.

The only place I’ve seen this documented is here:

https://youtu.be/3SCdzzD0PdQ?t=687

Thank you, looks great!

@coreyjv I've tried and tried to get this to work, I asked a question on SO too: https://stackoverflow.com/questions/55390461

I'm stuck with a {"message": "Missing Authentication Token"} . If you could take a look at my question and let me know what i'm missing, that would be greatly appreciated!

@rodolphito I responded to your SO question.

I came here through several Google search and found out that we actually don't have it in the sdk.
So I've tried to "patch", and it works fine, hope the Aws team will add this api soon!

1) Create file patch.js

require('aws-sdk/lib/node_loader');
var AWS = require('aws-sdk/lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;

apiLoader.services['apigatewaymanagementapi'] = {};
AWS.ApiGatewayManagementApi = Service.defineService('apigatewaymanagementapi', ['2018-11-29']);
Object.defineProperty(apiLoader.services['apigatewaymanagementapi'], '2018-11-29', {
    get: function get() {
        var model = {
            "metadata": {
                "apiVersion": "2018-11-29",
                "endpointPrefix": "execute-api",
                "signingName": "execute-api",
                "serviceFullName": "AmazonApiGatewayManagementApi",
                "serviceId": "ApiGatewayManagementApi",
                "protocol": "rest-json",
                "jsonVersion": "1.1",
                "uid": "apigatewaymanagementapi-2018-11-29",
                "signatureVersion": "v4"
            },
            "operations": {
                "PostToConnection": {
                    "http": {
                        "requestUri": "/@connections/{connectionId}",
                        "responseCode": 200
                    },
                    "input": {
                        "type": "structure",
                        "members": {
                            "Data": {
                                "type": "blob"
                            },
                            "ConnectionId": {
                                "location": "uri",
                                "locationName": "connectionId"
                            }
                        },
                        "required": ["ConnectionId", "Data"],
                        "payload": "Data"
                    }
                },
                "DeleteConnection": {
                    "http": {
                        "requestUri": "/@connections/{connectionId}",
                        "responseCode": 200,
                        "method": "DELETE"
                    },
                    "input": {
                        "type": "structure",
                        "members": {
                            "ConnectionId": {
                                "location": "uri",
                                "locationName": "connectionId"
                            }
                        },
                        "required": [
                            "ConnectionId"
                        ]
                    }
                }
            },
            "shapes": {}
        }
        model.paginators = {
            "pagination": {}
        }
        return model;
    },
    enumerable: true,
    configurable: true
});

module.exports = AWS.ApiGatewayManagementApi;

2) Apply the patch

const AWS = require('aws-sdk');
require('./patch.js');

3) Use it!

  const apigwManagementApi = new AWS.ApiGatewayManagementApi({
    apiVersion: '2018-11-29',
    endpoint: '---ehh I dont want to show it here---'
  });
  drop = async (connectionId) => {
    await apigwManagementApi.deleteConnection({ ConnectionId: connectionId }).promise();
  }

Considering the documentation claims that there _are_ methods getConnection and deleteConnection, and these methods don't actually exist, this seems to be a bug, and not a feature request.

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ApiGatewayManagementApi.html#getConnection-property

This ticket can be closed as they now exist :+1:

Was this page helpful?
0 / 5 - 0 ratings