Skip to content
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

Lift restriction to use JSON for communication #20

Closed
Drenmi opened this issue Feb 6, 2020 · 3 comments
Closed

Lift restriction to use JSON for communication #20

Drenmi opened this issue Feb 6, 2020 · 3 comments

Comments

@Drenmi
Copy link

Drenmi commented Feb 6, 2020

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?

@Drenmi Drenmi changed the title Loosen restriction to use JSON for communication Lift restriction to use JSON for communication Feb 6, 2020
@ioquatix
Copy link
Member

ioquatix commented Feb 6, 2020

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
Copy link
Member

ioquatix commented Feb 6, 2020

Also agreed on using msgpack! It's great!

@Drenmi
Copy link
Author

Drenmi commented Feb 6, 2020

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!

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