IC WebSocket: Stable Release

New release!

Today we’re releasing new IC WebSocket versions that affect both the SDKs and the gateway.

New versions

SDKs:

Gateway:

What’s changed

SDKs

We suggest you to update all your SDKs to the latest version.

You don’t have to specify the gateway principals anymore in the parameters, the protocol handles it for you under the hood!

If you’re using Rust in your canister, change the initialization of the WsInitParams to:

- let params = WsInitParams::new(handlers, vec![gateway_principal]);
+ let params = WsInitParams::new(handlers);

If you’re using Motoko in your canister, import the new modules from the CDK and initialize it with:

import IcWebSocketCdk "mo:ic-websocket-cdk";
+ import IcWebSocketCdkState "mo:ic-websocket-cdk/State";
+ import IcWebSocketCdkTypes "mo:ic-websocket-cdk/Types";

actor class YourCansiter() {
+   let params = IcWebSocketCdkTypes.WsInitParams(null, null, null);

-   var ws_state = IcWebSocketCdk.IcWebSocketState([gateway_principal]);
+   let ws_state = IcWebSocketCdkState.IcWebSocketState(params);

    // on_open, on_message, on_close callbacks defined here...

-   let handlers = IcWebSocketCdk.WsHandlers(
+   let handlers = IcWebSocketCdkTypes.WsHandlers(
      ?on_open,
      ?on_message,
      ?on_close,
    );

-   let params = IcWebSocketCdk.WsInitParams(
-     handlers,
-     null,
-     null,
-     null,
-   );

-   let ws = IcWebSocketCdk.IcWebSocket(ws_state, params);
+   let ws = IcWebSocketCdk.IcWebSocket(ws_state, params, handlers);

    // expose the ws_... methods here
};

That’s all you have to do!

You can also follow the updated tutorial for reference.

Gateway

  • big refactoring of the gateway codebase, which involves the use of dashmap. Now a single state is shared across client session handlers and pollers.
  • the polling logic has been changed, so that if there are still messages in the canister’s queue, the poller immediately polls them, without waiting for the next polling iteration.
  • errors returned from the replica/canister during polling are handled in a better way, so that the poller is not always terminated when they occur
8 Likes