Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event subscription (TS sdk) #4249

Merged
merged 97 commits into from
Sep 8, 2022
Merged

Event subscription (TS sdk) #4249

merged 97 commits into from
Sep 8, 2022

Conversation

stella3d
Copy link
Contributor

@stella3d stella3d commented Aug 23, 2022

Add event subscription & general websocket functionality to TS SDK.

Basic use

Event filter type mirrors the Rust SuiEventFilter

This shows subscribing to all DevnetNFT events.

const provider = JsonRpcProvider('http://localhost:9000');
const filter = {All: [{EventType:  "MoveEvent"}, {Package: "0x2"}, {Module: "devnet_nft"}]};

// subscribing to an event returns a SubscriptionId
const devNftSub = await provider.subscribeEvent(filter, (event: SuiEventEnvelope) => {
    console.log(event);
    // handle subscription notification message here
});

// later, to unsubscribe 
const subFoundAndRemoved = await provider.unsubscribeEvent(devNftSub);

Testing

Easiest way to test the functionality is:

  1. start a local Sui network, run genesis & start, then start sui node
cargo build --release
cd /target/release
sui genesis 
sui start
sui-node
  1. to subscribe to DevNetNFT events as shown above in Basic Use, and log the results.

Socket Messages

As an example, here's the sequence of messages that results from:

  • socket connect
  • subscribe to devnetNFT events
  • receive 2 notifications for the subscription (lower half shows the detailed notification message)
  • unsubscribe from our subscription

image

Connection Maintanence

We delay connecting until a websocket is needed, to avoid spamming the node with connections.

If the connection gets closed with active subscriptions and we reconnect, we re-subscribe those subscriptions.

We will likely need to add some rpc method for doing keep-alive / heartbeat requests in the future, so we can ensure connections stay open longer.

@stella3d
Copy link
Contributor Author

stella3d commented Sep 7, 2022

@666lcz i know accessing the underlying .on('message') seems weird but i spent quite a bit more time than i would like on it, including reading all of the code of several other projects (which is where i got the idea to cast and access the underlying event emitter)
Solana's, for example, uses a different notion of notifications - they subscribe to named events.

We could use this approach if we have named events, and i could do like:

this.wsClient.on(
    'eventNotify',
    this.onSocketMesage.bind(this),
);

The rpc-websockets library does not appear to expose the underlying on('message') callback.

@stella3d stella3d dismissed 666lcz’s stale review September 7, 2022 04:41

need a fresh review

@@ -421,4 +430,15 @@ export class JsonRpcProvider extends Provider {
);
}
}

async subscribeEvent(
filter: SuiEventFilter,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the desired behavior if filter == {All: []}, will it get subscribed all the events in the Network? cc @longbowlu

sdk/typescript/src/rpc/websocket-client.ts Outdated Show resolved Hide resolved
sdk/typescript/src/rpc/websocket-client.ts Show resolved Hide resolved
sdk/typescript/src/rpc/websocket-client.ts Outdated Show resolved Hide resolved
Stella Cannefax and others added 3 commits September 7, 2022 14:10
@stella3d stella3d requested a review from 666lcz September 7, 2022 23:17
@stella3d stella3d enabled auto-merge (squash) September 8, 2022 21:42
@stella3d stella3d merged commit e046e01 into main Sep 8, 2022
@stella3d stella3d deleted the tx-subscribe branch September 8, 2022 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants