jpsamaroo has it right.
What you put together there is an elegant and useful example which we should put in the ‘examples’ folder.
If you’re planning to use this in a relay server, I’d suggest these modifications to get fewer restarts and some traceability:
@async WebSockets.open ...> @async WebSockets.with_logger(WebSocketLogger()) do
write -> writeguarded
read -> readguarded
@wslog currentmessage