Accessing Nats from a web page, via JavaScript

2,038 views
Skip to first unread message

maurizio de magnis

unread,
Sep 8, 2015, 12:43:26 PM9/8/15
to nats
Hi everybody,

first of all let me say that I really like the overall simplicity of Nats, it's just a joy to toy around with it (I've played with the ruby gem so far).

In HTTP-land, especially in H2, we have Server Sent Events and ajax calls but I'd like to know if some of you have already imagined of allowing a JS client do the pub/sub dance with Nats, something like Faye [0] do.

I guess that given Gnatsd uses a text based protocol over TCP I could add a middleman server between JS clients and the Gnatsd endpoint[s] but that's likely not feasible because then I'd have to implement a lot of things the Gnatsd endpoint already cover.

Any thoughts?

Tyler Treat

unread,
Sep 8, 2015, 3:13:39 PM9/8/15
to nats
We are consuming NATS from Dart and JavaScript clients by setting up a thin proxy server which exposes a websocket/sockjs endpoint to the client and connects to gnatsd. I suppose it would be feasible to expose websockets directly from gnatsd but that opens all sorts of questions like how do you authenticate client connections? Our proxy handles authentication and rate limiting clients to mitigate DoS attacks.

Derek Collison

unread,
Sep 8, 2015, 7:25:40 PM9/8/15
to nats
We have some designs in process with a customer to enable WS connections to NATS. Stay tuned, and of course this will be an OSS solution which we hope to get much feedback on.

Robert Nagy

unread,
Sep 9, 2015, 11:25:45 AM9/9/15
to nats
While waiting for proper ws support in Nats we've forked the javascript client to add web socket support and also created a proxy server.

You can find them here (we are just working on pushing to the repos so the changes might not be there right away):


There is also a Docker image for the proxy server:

Derek Collison

unread,
Sep 9, 2015, 8:40:58 PM9/9/15
to nat...@googlegroups.com
That's great!

--
You received this message because you are subscribed to the Google Groups "nats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to natsio+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Larry McQueary

unread,
Sep 9, 2015, 8:56:09 PM9/9/15
to nat...@googlegroups.com
We (Team NATS) are very enthused to see this and other NATS-WebSocket components being contributed (yes you, Raul!). It's also exciting to see how quickly these projects are being turned around. For us it validates the concept of NATS being a versatile, lightweight core nervous system for cloud native infrastructure.

Stay tuned -- we aim to keep contributing hooks that the community can hang stuff on :)

Raül Pérez

unread,
Oct 10, 2015, 11:40:29 PM10/10/15
to nats
OMG I've missed this post! :P
Been a bit busy with work lately, but I can explain here my research a little bit.

Basically I have two approaches on this problem:

1. Creating a proxy client in go ( gnatsd ).
2. Adding websockets support directly to gnatsd, firing up a new server on a different port ( like the monitor does ).

First option is easy to solve, but then could appear other problems like the number of connections splited on two 
servers, so monitoring would be harder.

Second approach would be perfect and the conversion between the two protocols ( websocket is message based and gnatsd 
is stream based ) is the real problem to solve to maintain the throughput, where NATS excels.

I've started the second approach on a separate branch here: https://github.com/repejota/gnatsd/commit/e512a3829195fe5de3e73925895d6bc165e6901f 
Currently I have the easy part, which is firing up a websocket server on a separate port and I'm researching the most efective way to read a websocket 
message and route it through gnatsd flow, but maintaining of course, its performance.

So ... as Larry and Derek says ... stay tuned! :D
Reply all
Reply to author
Forward
0 new messages