Skip to content

Instantly share code, notes, and snippets.

@praveen001
Last active January 26, 2020 14:40
Show Gist options
  • Save praveen001/a12c9cf3eb6e882011092305dd942d0e to your computer and use it in GitHub Desktop.
Save praveen001/a12c9cf3eb6e882011092305dd942d0e to your computer and use it in GitHub Desktop.
Serverless WebSockets with API Gateway and Golang Lambda
// Disconnect will receive the $disconnect requests
func Disconnect(request APIGatewayWebsocketProxyRequest) (interface{}, error) {
id := request.RequestContext.Authorizer.(map[string]interface{})["cognito:username"].(string)
connectionID := request.RequestContext.ConnectionID
RemoveSocket(id, connectionID)
return events.APIGatewayProxyResponse{
StatusCode: 200,
}, nil
}
// RemoveSocket will remove the id,connectionId socket from dynamodb
func RemoveSocket(id, connectionID string) {
input := &dynamodb.DeleteItemInput{
TableName: aws.String("go-ws-demo-sockets"),
Key: map[string]*dynamodb.AttributeValue{
"connectionId": &dynamodb.AttributeValue{
S: aws.String(connectionID),
},
"id": &dynamodb.AttributeValue{
S: aws.String(id),
},
},
}
db := dynamodb.New(GetSession())
_, err := db.DeleteItem(input)
if err != nil {
log.Fatalln("Unable to remove user socket map", err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment