Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force disconnecting an API Gateway WebSocket through Lambda #377

Open
villain-bryan opened this issue Nov 12, 2022 · 3 comments
Open

Force disconnecting an API Gateway WebSocket through Lambda #377

villain-bryan opened this issue Nov 12, 2022 · 3 comments

Comments

@villain-bryan
Copy link

I'm using the WebSocket Protocol for API Gateway to route Client requests to specific Lambda invocations. How can I use Lambda to close a WebSocket?

Example:

public APIGatewayV2ProxyResponseEvent handle(APIGatewayV2WebSocketEvent input) {
    // Print this WebSocket's connectionId
    System.out.println("connectionId: " + input.getRequestContext().getConnectionId());
    // Great, I have the connectionId .. but how can I close it from here?
}
@villain-bryan
Copy link
Author

villain-bryan commented Nov 13, 2022

Likewise, how can I send a packet to a specific WebSocket if I store that connectionId in DynamoDB? I'm basically wondering what the object is to create/recreate a WebSocket into a usable form from any Lambda given a connectionId, and haven't found any obvious way to do that.

All I've found is this reference https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html for accessing the connections, but it doesn't look convenient to work with.

@msailes
Copy link
Collaborator

msailes commented Nov 13, 2022

Amazon API Gateway's WebSocket support is event driven in design. It will send events on Connect, Disconnect and Message.

You must manage the connections and who it is connected to the other end. When you implement the Connect handler you will probably save a business id with the connection id. Then when you want to send a message to a specific entity, you can find the connection id from that id. You then invoke the API Gateway management API to send a message to a specific connection id.

This is an example you might find helpful.

https://github.com/aws-samples/simple-websockets-chat-app

@villain-bryan
Copy link
Author

Amazon API Gateway's WebSocket support is event driven in design. It will send events on Connect, Disconnect and Message.

You must manage the connections and who it is connected to the other end. When you implement the Connect handler you will probably save a business id with the connection id. Then when you want to send a message to a specific entity, you can find the connection id from that id. You then invoke the API Gateway management API to send a message to a specific connection id.

This is an example you might find helpful.

https://github.com/aws-samples/simple-websockets-chat-app

That reference is helpful, but I'm looking for a Java solution to manage the API Gateway WebSockets. Not sure how to do that - I was able to find some help via AmazonApiGatewayManagementApi, but seems to have an issue with Quarkus GraalVM build-native.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants