How to send a message in WebSockets 🔌 on the Internet Computer

It’s great that you tried our tutorial!

This is something that shouldn’t happen. Every time a new client connects, the on_open callback is called by the CDK on the canister. If this doesn’t happen, it means that the connection is still not fully established. This appears to be a bug, that we also have encountered and that we’re fixing. As a temporary workaround, reinstalling the canister should fix it (but you’re going to loose all the canister data, so be careful!).

We’re working on a more robust protocol for the IC WebSocket, that we’ll release soon. We’ll also update the tutorial accordingly. With this update, you’ll be able to use the client’s principal instead of that client_key. So, to answer your question:

No, you can’t with the current version. What you can do for now is to create a map client principal → client_key in order to find the right client_key to pass to the send_app_message function.

If you call the ws_send passing a specific client_key, the underlying IC WebSocket protocol takes care of delivering it to the right client and only that one.

P.S.: I’ve reached you out in DM if you want to have a call together, so that we can help you out better.

3 Likes