Skip to content

Example/doc for how to talk to a PubSub server #120

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

Closed
faern opened this issue Apr 20, 2017 · 15 comments
Closed

Example/doc for how to talk to a PubSub server #120

faern opened this issue Apr 20, 2017 · 15 comments

Comments

@faern
Copy link
Contributor

faern commented Apr 20, 2017

I don't find very much information how to talk to a server using the PubSub feature of this repository. Would it be possible to add an example or some docs on that?

I have followed the example file to set up a server, and it is running. But I don't know what send to it to register my client.

Some help with this would be awesome. Thanks.

@tomusdrw
Copy link
Contributor

If you have a running server from this example:
https://github.com/paritytech/jsonrpc/blob/a95e135017c3a29fbda5dad88dc62f8797792443/pubsub/examples/pubsub.rs

You need to issue a standard RPC with {"id":1,"jsonrpc":"2.0","method":"subscribe_hello"} and then you should start seeing subscriptions coming to your client.
The transport used in the example is TCP, so you can for instance use netcat to connect to the server:

$ netcat localhost 3030 -
{"id":1,"jsonrpc":"2.0","method":"subscribe_hello"}
{"jsonrpc":"2.0","result":5,"id":1}
{"jsonrpc":"2.0","method":"hello","params":[10]}
{"jsonrpc":"2.0","method":"hello","params":[10]}
{"jsonrpc":"2.0","method":"hello","params":[10]}

@faern
Copy link
Contributor Author

faern commented Apr 20, 2017

I'm running the example using macros at https://github.com/paritytech/jsonrpc/blob/a95e135017c3a29fbda5dad88dc62f8797792443/macros/examples/pubsub-macros.rs

But I get this:

$ netcat localhost 3030 -
{"id":1,"jsonrpc":"2.0","method":"hello_subscribe"}
{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":1}

@faern
Copy link
Contributor Author

faern commented Apr 20, 2017

I did get your example to work with the example not using macros. But using the macros would be sweet.

@tomusdrw
Copy link
Contributor

Hmm, you're right. It turned out that the alias invocation in macros was invalid (it was overriding the implementation with alias to non-existent method).

Please try with #121 or remove alias in the example.

@faern
Copy link
Contributor Author

faern commented Apr 20, 2017

That worked better! Thanks. Now I get:

$ netcat localhost 3030 -
{"id":1,"jsonrpc":"2.0","method":"hello_subscribe","params":[10]}
{"jsonrpc":"2.0","result":0,"id":1}
{"jsonrpc":"2.0","method":"hello","params":{"result":"Hello World!","subscription":0}}
{"jsonrpc":"2.0","method":"hello","params":{"result":"Hello World!","subscription":0}}
{"jsonrpc":"2.0","method":"hello","params":{"result":"Hello World!","subscription":0}}
....

Now I will just move on to try to get this running on the WebSocket server.

@tomusdrw
Copy link
Contributor

You can have a look how we wrapped the ws::Sender into mpsc::Sender:
https://github.com/paritytech/parity/pull/5456/files#diff-047350db0c9925ab2d582dfcd955f0a4R173

I'm considering moving this into this crate though.
If you manage to get the example running feel free to contribue it to ws/examples :)

@faern
Copy link
Contributor Author

faern commented Apr 21, 2017

I got the server running. But when I try to communicate with in using a python websocket client I get this response:

{"jsonrpc":"2.0","error":{"code":-32090,"message":"Subscriptions are not available on this transport."},"id":1}

Is there anything I can do about that? Would the ws crate need some modifications or which part is it that stops subscriptions to work over ws?

@tomusdrw
Copy link
Contributor

Could you post the example somewhere? For PubSub to work you need to ensure that MetadataExtractor will create a Session object (that definitely needs more detailed documentation).

@faern
Copy link
Contributor Author

faern commented Apr 21, 2017

Ah. My bad. I removed the call to session_meta_extractor. Now I'm trying to figure out how to make that work. Looking at the code you sent, but it uses rpc::PubSubSession which is not part of this repository.

@faern
Copy link
Contributor Author

faern commented Apr 21, 2017

It kind of escalates with the WsRpcExtractor and the Sender struct implementing Future etc. There is hopefully a simpler way that I have not found yet.

@tomusdrw
Copy link
Contributor

It's actually just a re-export of jsonrpc_pubsub::Session. I'll prepare an example today for you :)

@faern
Copy link
Contributor Author

faern commented Apr 24, 2017

Thank you. That would be awesome.

@tomusdrw
Copy link
Contributor

Please have a look at #126. I've moved the implementation of mpsc::Sender to this crate.

@faern
Copy link
Contributor Author

faern commented Apr 24, 2017

@tomusdrw Thanks for the help! Got it working and also got an example based on macros working. Merging that PR would solve/close this issue.

@tomusdrw
Copy link
Contributor

Awesome! Glad to hear that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants