socketry / async-websocket

Asynchronous WebSocket client and server, supporting HTTP/1 and HTTP/2 for Ruby.
MIT License
166 stars 18 forks source link

Lift restriction to use JSON for communication #20

Closed Drenmi closed 4 years ago

Drenmi commented 4 years ago

Hello, @ioquatix!

Firstly, thanks for all your work on the async project. I am very excited to see progress in these areas of the Ruby ecosystem. ❤️

Now for my question. I was playing around with async-websocket over the weekend, and the first experiment resulted in an error, because the message I sent to the websocket server (just a single word) failed to parse as JSON.

Is the restriction to send and receive JSON intentional? If so, maybe it should be documented down in the README. If not, I would be open to help add configuration options for other formats like plain text, MessagePack, etc.

What do you think?

ioquatix commented 4 years ago

I don't know a huge amount about how websockets are used in practice.

I've done a few things in production with them - chat clients/servers, interactive websites, etc.

However, in many of those cases we used something like Socket.io which use JSON across the wire. It was my assumption that using JSON across the wire is pretty typical.

But, I could be wrong?

So, feel free to correct me.

Regarding your questions, you can actually use whatever format you want. You just need to make your own handler.

Basically, make a subclass and overload https://github.com/socketry/async-websocket/blob/master/lib/async/websocket/connection.rb#L73-L79

Then on the client and/or server, specify the connection class as the handler:

https://github.com/socketry/async-websocket/blob/master/lib/async/websocket/client.rb#L89

https://github.com/socketry/async-websocket/blob/master/lib/async/websocket/server.rb#L31

We should probably write some more documentation for this.

The point of the handler is not just to be serialization, but any specific per-connection state you want to manage.

Let me know if that's enough for you to start with. I don't have any specific examples, but it would be great to add some!

ioquatix commented 4 years ago

Also agreed on using msgpack! It's great!

Drenmi commented 4 years ago

To be fair, I haven't worked a lot with WebSockets in production settings outside of ActionCable. 🙂

Then on the client and/or server, specify the connection class as the handler

Ah! I missed the fact that the handler can be injected.

The point of the handler is not just to be serialization, but any specific per-connection state you want to manage.

This makes perfect sense to me. I think knowing that the handler can be injected, I'm against my own idea of merely configuring the format, in favour of the higher level abstraction. 🙂

Let me know if that's enough for you to start with. I don't have any specific examples, but it would be great to add some!

This definitely points me in the right direction. I can open a PR to add some examples to the README once I play around some more with it.

Thanks, @ioquatix!