paritytech / jsonrpsee

Rust JSON-RPC library on top of async/await
MIT License
624 stars 167 forks source link

Question: Disconnect ws connection from server side #1448

Closed shunsukew closed 1 week ago

shunsukew commented 1 week ago

Is it possible to disconnect ws connection from server side by using jsonrpsee api?

What I'm thinking of is disconnect ws connection from server side (not from client side) under specific situations.

shunsukew commented 1 week ago

As a background, software that proxying subscriptions (for e.g. Subway), proxy silently stop sending responses to clients when upstream ws connection is disconnected.

To avoid such situation, what I came up with are

niklasad1 commented 1 week ago

Hey @shunsukew

Is it possible to disconnect ws connection from server side by using jsonrpsee api?

There is no dedicated API in jsonrpsee for the server-side the close down connections but it's possible to use to the low-level API with rpc middleware (which is a bit complicated to use)

There is a similar issue regarding this https://github.com/paritytech/jsonrpsee/issues/1427

When talking about subscriptions specifically I just want clarify that a few options exist to inform the client without closing the connection:

shunsukew commented 1 week ago

Hello, Thank you for the information!

Reject the subscription i.e, call Subscription::reject which just returns an error the origin RPC call

I was looking for a way to disconnect after accepting.

Send a special "error notification" or "application specific notification" before closing down subscription

In our usecase, just proxying standard subscribe calls (e.g. eth_subscribe newHeads), quite hard to add custom error notification, client sdk doesn't understand it because it's not eth/substrate standards.

Thank you for the sharing low level apis! I think this is the only way to disconnect after accepting PendingSubscription. For now, we have 2 different ws connection (client <> proxy and proxy <> upstream), and decided to re-subscribe after re-established the upstream connection, while keeping client side ws connection stay connected. Possibly, will find a way to use those low level apis to disconnect connection itself.