tokio-line
tokio-line copied to clipboard
Add a mini-chat example
Preamble: Carl, this is a followup request to our IRC discussion yesterday. I tried to implement this example below and failed and I think this would be a useful example to have for tokio - so far all examples are strictly request -> response based, while this one requires some state.
Add an example that acts as a mini-chat server. Clients could use telnet localhost port
(or alternatively use the already implemented client in this repo) to connect to it and every line they write is broadcast to everybody who is right now connected to the server .
This would benefit in the following ways:
- I'd show how to reuse a transport (i.e. LineTransport) for multiple distinct server implementations. This makes it clearer that Transport can be swapped out without affecting the logic of the communication.
- I'd show how a transport can be shared between a
pipeline::Server
and something external that holds state (i.e. the currently connected clients) OR how to implement an alternative pipeline::Server that holds state and brokers for multiple transports. This is the point that I failed at. How to write this?
Definitely a good idea 👍 I'll see if I can try to work on doing something like that.
Carl, first congrats on forming the tokio-core group - that is exciting news and gives this project a lot more weight. It is hard to overstate your importance for Rust to become a viable choice in production systems!! Thanks.
I just want to ping this issue - I am unable to figure out which design should work when using Tokio for something like this example and I am sorta blocked on it on my main tokio project - could you whip an example together real quick or at least outline the approach you would take?
So, ideally, this would be implemented using something like alexcrichton/futures-rs/#52. The details of which are a little tricky. I will probably wait a bit to see if the broadcast issue goes anywhere before I try tackling it :)
I've written something similar here https://github.com/arjsin/tokio-broadcast-example using tokio-core. Temporarily, it maybe used until futures-rs broadcast comes.