Crates.io | ic-websocket-cdk |
lib.rs | ic-websocket-cdk |
version | 0.4.1 |
source | src |
created_at | 2023-10-17 17:02:26.682536 |
updated_at | 2024-10-31 14:43:42.659132 |
description | IC Websocket Canister Development Kit |
homepage | https://docs.rs/ic-websocket-cdk |
repository | https://github.com/omnia-network/ic-websocket-cdk-rs |
max_upload_size | |
id | 1005940 |
size | 183,439 |
This repository contains the Rust implementation of IC WebSocket CDK. For more information about IC WebSockets, see IC WebSocket Gateway.
You can install the library by running:
cargo add ic-websocket-cdk
Refer to the ic_websocket_example repository for an example of how to use the library.
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.
The ic-websocket-cdk library implementation can be found in the src/ic-websocket-cdk folder.
There are two types of tests available:
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
MIT License. See LICENSE.
Feel free to open issues and pull requests.