12 releases

0.4.1 Oct 31, 2024
0.4.0 Jul 21, 2024
0.3.4 Feb 26, 2024
0.3.2 Dec 15, 2023
0.3.0 Nov 21, 2023

#414 in Magic Beans

Download history 87/week @ 2024-07-22 11/week @ 2024-07-29 9/week @ 2024-09-16 15/week @ 2024-09-23 2/week @ 2024-09-30 16/week @ 2024-10-07 122/week @ 2024-10-28 25/week @ 2024-11-04

147 downloads per month

MIT license

185KB
3.5K SLoC

ic-websocket-cdk-rs

This repository contains the Rust implementation of IC WebSocket CDK. For more information about IC WebSockets, see IC WebSocket Gateway.

Installation

You can install the library by running:

cargo add ic-websocket-cdk

Usage

Refer to the ic_websocket_example repository for an example of how to use the library.

Candid interface

In order for the frontend clients and the Gateway to work properly, the canister must expose some specific methods in its Candid interface, between the custom methods that you've implemented for your logic. A valid Candid interface for the canister is the following:

import "./ws_types.did";

// define here your message type
type MyMessageType = {
  some_field : text;
};

service : {
  "ws_open" : (CanisterWsOpenArguments) -> (CanisterWsOpenResult);
  "ws_close" : (CanisterWsCloseArguments) -> (CanisterWsCloseResult);
  "ws_message" : (CanisterWsMessageArguments, opt MyMessageType) -> (CanisterWsMessageResult);
  "ws_get_messages" : (CanisterWsGetMessagesArguments) -> (CanisterWsGetMessagesResult) query;
};

This snipped is copied from the service.example.did file and the types imported are defined in the ws_types.did file.

To define your message type, you can use the Candid reference docs. We suggest you to define your message type using a variant, so that you can support different messages over the same websocket instance and make it safe for future updates of your types.

To automatically generate the Candid interfaces for your canister, have a look at this article.

Development

The ic-websocket-cdk library implementation can be found in the src/ic-websocket-cdk folder.

Testing

There are two types of tests available:

  • Unit tests: tests for CDK functions, written in Rust and available in the unit_tests folder.
  • Integration tests: for these tests the CDK is deployed to a test canister. These tests are written in Rust and use PocketIC under the hood. They are available in the integration_tests folder.

There's a script that runs all the tests together, taking care of setting up the environment (Linux only!) and deploying the canister. To run the script, execute the following command:

./scripts/test.sh

License

MIT License. See LICENSE.

Contributing

Feel free to open issues and pull requests.

Dependencies

~3–11MB
~125K SLoC