Last active
January 26, 2020 14:40
-
-
Save praveen001/a12c9cf3eb6e882011092305dd942d0e to your computer and use it in GitHub Desktop.
Serverless WebSockets with API Gateway and Golang Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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