Using rust-websocket with Iron

2k views Asked by At

For a high-performance websocket server, ideally I want to reorient Iron to listen websockets instead of http(s).

Is it possible to use rust-websocket with iron, or does it not make sense to use both together?

If it is possible, how can I realize it?

3

There are 3 answers

0
AndrewBrinker On

It sound like you want to swap out Hyper inside of Iron for rust-websocket. This is likely to be difficult, if it is even possible at all. Iron is heavily integrated with Hyper, and the entire design is built around working over HTTP(S). If it's really something you want to do, it may be worthwhile to reach out to the Iron developers to see about the possibility of allowing the communication interface to be swappable, but I don't know how likely it is that they would be receptive to the idea.

0
IceyEC On

I'm looking at doing using both Iron and rust-websocket in a single project and the architecture that I've come to includes having the websocket listen on a separate port. I can mask this in deploy with Nginx in front proxying back to the specific ports

0
Ultrasaurus On

Since your goal is to create a high-performance websocket server, then starting with an HTTP server, like Iron, probably does not make sense. (Iron is based on Hyper, which advertises itself as "a fast and correct HTTP implementation"). I would recommend looking at tokio which was designed as "an asynchronous, event driven platform" and is used by Hyper and Iron.

WebSockets require a different protocol that creates a two-way interactive communication session. From Mozilla docs:

you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

So, if you don't need HTTP, then starting with a server that is focused on request/response is likely to introduce more complexity than benefit. While iron websocket issue is still open, recent comment notes:

Personally I think it's pretty difficult to fit websocket into Iron's request-middleware-response model. I haven't seen elegant abstraction in other languages for this.

If you really want to explore using WebSockets with Iron, you would need to extend hyper to support WebSockets (good discussion here) and then access lower-level hyper connection (explained in iron issue #478). After establishing the connection, a WebSocket library would be useful (though rust-websocket appears to be no longer maintained).