Let's make Websocket documentation even better
I think perhaps a separate page would be best? If we can agree, I will make a PR.
Here are the things I was struggling with and now I figured them out so I feel it would be useful to have them documented. It doesn't help the Serverless documentation on Websocket itself leaves open questions.
First, if you want to use return from your handler to send a message then serverless.yml needs to have a routeResponseSelectionExpression, the simplest working version is:
events:
- websocket: $connect
- websocket: $disconnect
- websocket:
route: $default
routeResponseSelectionExpression: $default
Second, if you want to send message(s) from the class instead of returning them then the SimpleWebsocketClient class is tailor made for that and the various method arguments all have corresponding getters on the WebsocketEvent class. It is not a class to test your websocket AWS Lambda function with, for example textalk/websocket works for that (perhaps mention wscat as well). I feel perhaps the class name is a bit misleading here. Anyways, here's an example:
public function handleWebsocket(WebsocketEvent $event, Context $context): HttpResponse {
$client = new SimpleWebsocketClient($event->getApiId(), $event->getRegion(), $event->getStage());
$client->message($event->getConnectionId(), 'Hello from AWS Lambda');
}
Of course other connections could be used to as well if their connection id was stored somewhere but the constructor remains the same. Perhaps a SimpleWebsocketClient::createFromEvent() factory method would be helpful? Let me know.
Sorry for the delay, any documentation improvement is most welcome!
Closing because of inactivity.