paritytech / jsonrpc

Rust JSON-RPC implementation
MIT License
791 stars 274 forks source link

Implement named params for pubsub subscribe client #659

Closed AsamK closed 1 year ago

AsamK commented 2 years ago

The library currently supports the params = "named" derive attribute for rpc methods. But it doesn't support it for pubsub subscribe methods. This PR adds params to the allowed attributes for pubsub and adds the params generation code to send out named and raw params, in the same way as it was already implemented for normal rpc methods.

The following code will work with this PR:

#[rpc(client)]
pub trait Rpc {
    #[pubsub(
        subscription = "receive",
        subscribe,
        name = "subscribeReceive",
        params = "named"
    )]
    fn subscribe_receive(&self, _: Self::Metadata, _: Subscriber<Value>, some_param: Option<String>);

    /// Unsubscribe from hello subscription.
    #[pubsub(subscription = "receive", unsubscribe, name = "unsubscribeReceive")]
    fn unsubscribe_receive(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
}

and will send out this JSON-RPC message:

{"jsonrpc":"2.0","method":"subscribeReceive","params":{"some_param":"value"},"id":0}