1
votes

I connect a client (or a couple of clients) to the websockets endpoint in API Gateway.

Using wscat

Then, I try to post a message back to the client using these guidelines: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html

// Send sends a message to a connection ID.
func Send(domain, stage, connectionID, message string) (events.APIGatewayProxyResponse, error) {
    session := session.Must(session.NewSession())
    endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID)
    apiClient := apigatewaymanagementapi.New(session, aws.NewConfig().WithEndpoint(endpoint))
    connectionInput := apigatewaymanagementapi.PostToConnectionInput{
        ConnectionId: aws.String(connectionID),
        Data:         []byte(message),
    }
    _, err := apiClient.PostToConnection(&connectionInput)

    if err != nil {
        fmt.Println(err.Error())

        return events.APIGatewayProxyResponse{StatusCode: 500}, err
    }

    return events.APIGatewayProxyResponse{StatusCode: 200}, nil
}

It doesn't matter whether I invoke the Send function locally or a client sends a message and API Gateway invokes my publish Lambda where I loop through the connections and invoke Send for each of them. The result is always the same.

NotFoundException: 
    status code: 404, request id: 7bb1546a-c2a7-4e98-92a0-fcc7ae175d7c

Things I've tried:

  • Escaped @connections and the actual connectionID
  • Made sure the client connection hasn't timed out
  • Made sure I have the correct AWS credentials in my environment variables
  • Made sure my Lambda has permissions to invoke API Gateway
  • Made sure the endpoint is in the correct format: https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}

How can I successfully send messages to the clients?

1

1 Answers

1
votes

Turns out this line

endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID)

needs to turn into this

endpoint := fmt.Sprintf("https://%s/%s/", domain, stage)