Any opinion on services being able to connect to nats using WS protocol?

Hi folks,

I have a use-case that seems perfect for Resgate. I have several IoT devices lying around and I’d like to connect those as part of a mesh. These devices are on separate networks but they all have connections to the internet.

So, I’d like to be able to write RES service daemon that talks to Resgate via websocket protocol instead of talking to NATS directly. Correction: I’d like the RES service to talk to NATS via Websocket.

I think this is doable since the latest NATS server is able to speak WS (based on GitHub - nats-io/nats.ws: WebSocket NATS).

What do you guys think of this as an idea? I am currently looking around go-res project to see if I could hack that in as an experiment.

Hi, and welcome to the forum!

Using Resgate with IoTs works quite well :slight_smile:

But having your “server” IoTs connecting directly to Resgate doesn’t really make sense. So, as you mention NATS WebSocket, I assume you rather mean to have them connect to NATS server through WebSocket. Correct me if I misunderstood!

It is doable. If the IoT have their RES service daemon written in Go, the go-res can be used.
You can use the func (*Service) Serve which takes a type Conn interface. So, if you have a NATS WebSocket client for Go, you can make a wrapper that implements the go-res Conn interface, and pass it to Serve.

So, absolutely doable.

But the biggest part is the NATS WebSocket client implementation. Is there one already? Will you have to write your own based on the javascript version?
I don’t know how complex that protocol is. Hopefully it is straight forward.

Though, any reason why the IoT’s are not using an ordinary NATS client to connect to NATS? Is it to get through firewalls easier?

/Samuel

Thank you for the quick reply. Yes, “have them connect to NATS server through WebSocket” is indeed what I have in mind.

Thank you for pointing at the Conn interface, that’s is indeed what I am looking for, and thank you for making it an interface already.

Is there a NATS websocket client in Go? Great question. But surely it’s not super hard considering projects like these (GitHub - orus-io/nats-websocket-gw: A websocket to NATS gateway) (GitHub - orus-io/elm-nats: A Elm client for the NATS protocol) exist long ago.

I can probably play around with the Node.js example first since nats.ws already exists.

As for your last question, going through firewall easier is indeed the primary goal. IoT devices tend to have all sorts of strange firewall setup, but they always have outgoing internet connection.