njh / ruby-mqtt

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.
http://www.rubydoc.info/gems/mqtt
MIT License
541 stars 135 forks source link

MQTT Broker via Websockets #77

Closed sloanwolf closed 8 years ago

sloanwolf commented 8 years ago

I'm using the Paho JS client and have my mosquitto broker setup to use websockets but when I try to connect I get:

MQTT::ProtocolException: Failed to read byte from socket

Would you recommend a better way to go about this or could we get websocket support as well like the Paho JS library does. I can't seem to find anything for ruby that supports this.

njh commented 8 years ago

No, there is no support for WebSockets - and it is not something I was planning to add support for either.

What is your use case? Why would you not just use the proper protocol?

sloanwolf commented 8 years ago

I'm using React-Native JS (node environment), tried a bunch of clients but couldn't get any of them working so I got Paho JS library working in my node environment and it was good enough over Mosquitto w/ Websockets enabled until I needed a Ruby consumer to handle some server-side processing for certain events.

I'm refactoring an app over from HTTP API to MQTT and am using both at the moment, MQTT mainly for chat.

njh commented 8 years ago

Hi,

As far as I am aware the main benefit of WebSockets is to enforce the browser security restrictions for TCP sockets.

I am not sure what the benefits are outside of a browser environment?

nick.

sloanwolf commented 8 years ago

Yeah I'm a little new to pub/sub and did a lot of research, from what I saw I thought MQTT could scale better and was more lightweight for mobile applications than WebSockets.

I've used websockets before for a browser application. There is a module that utilizes MQTT native clients for iOS and Android but the author said it wasn't for production use and that a JS client should be used instead, I had it semi-working though so might give it another shot, would prefer to use native MQTT. I might test out a websocket implementation though and compare the weight/concurrency of each maybe. If I use websockets I could process and echo back responses instead of having a separate consumer to process events on the server-side.

Thanks for your comments. Looks like I just have more research and benchmarking to do.

sloanwolf commented 8 years ago

Just for sake of closure if anybody finds this with a similar problem. I thought I read something saying it wasn't possible to run mosquitto with mqtt and websockets but you can.

I'm using version Mosquitto 1.4.8 with this config:

#mqtt port
listener 1883

#the websocket port
listener 9001 127.0.0.1
protocol websockets

Then you can just connect using any MQTT w/ Websocket JS client and I'm connecting from the server using this ruby-mqtt gem as a consumer.

andresespinosapc commented 3 years ago

I have a legitimate use case. My mqtt server is on Heroku and it only accepts one port opened. The only way I found to have an http and mqtt server in the same port is using ws protocol instead of mqtt, so I have to use ws.