In our IC WebSocket JS SDK, we’re trying to make it possible for developers to not care about serialization and deserialization of application messages manually, while at the same enforcing them to implement a strong typing system between their canisters and their clients.
To do so, we require the generated actor in the parameters of the IcWebSocket
constructor and extract the developer’s application message type from the ws_message
method using this function:
Then, we’re using the obtained IDL type to decode the WebSocket message content coming from the canister:
However, if in the tests everything goes fine (using a custom created actor), when deploying it on the example, it gives some errors
-
the
extractApplicationMessageIdlFromActor
function throws the error:Application message type must be optional in the ws_message arguments
which means that the extracted type is not an instance of
OptClass
, while instead the generatedidlFactory
has the correctIDL.Opt
type.As a work around to this error, I’ve replaced the instanceof check with:
- if (!(applicationMessageArg instanceof IDL.OptClass)) { + if (!applicationMessageArg.name.startsWith("opt")) {
Is it robust enough?
-
after fixing the previous error, the
IDL.decode
call throws:Error: type mismatch: type on the wire rec_4, expect type record {text:text; timestamp:nat64} at Ba.checkType
(example’s application message type declaration here)
Should we extract the IDL type in a different way? What are we missing?