tokio-line icon indicating copy to clipboard operation
tokio-line copied to clipboard

Add a mini-chat example

Open SirVer opened this issue 8 years ago • 4 comments

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?

SirVer avatar Aug 21 '16 11:08 SirVer

Definitely a good idea 👍 I'll see if I can try to work on doing something like that.

carllerche avatar Aug 23 '16 21:08 carllerche

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?

SirVer avatar Aug 27 '16 09:08 SirVer

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 :)

carllerche avatar Aug 30 '16 04:08 carllerche

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.

arjsin avatar Oct 01 '16 14:10 arjsin