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

Support WebSocket Connectivity #315

Closed
pldubouilh opened this issue Aug 1, 2016 · 40 comments
Closed

Support WebSocket Connectivity #315

pldubouilh opened this issue Aug 1, 2016 · 40 comments

Comments

@pldubouilh
Copy link

pldubouilh commented Aug 1, 2016

Hi,

At @gretaio, we need our signaling server to talk with web browsers, and in order to perform this, we setup a small proxy to gateway websocket to tcp so it can talk to nats.

I saw on the todolist that you plan on adding a websocket strategy, and that's something we would greatly appreciate as that'd basically half the number of connections we need to have open 👍.

So, would you be open to a PR regarding this?

@derekcollison
Copy link
Member

Always open to PRs, have you looked at the NGINX plugin though? That may fit the bill.

https://github.com/nats-io/nginx-nats

@pldubouilh
Copy link
Author

According the last tests we did, nginx can only forward and keep a websocket connection open, and it still needs a server to operate the full websocket handshake…

Alright then, we'll see what we can do !

@derekcollison
Copy link
Member

Thanks for the information. We have a strategy session this week on NATS, so will discuss for sure.

@pldubouilh
Copy link
Author

Alright, so we have integrated our gateway code into nats. So basically for each ws connection we have incoming, we pop a tcp connection to nats. It's close to what we had before, but this time we wrote it into nats.

It's far from being an ideal solution (it's more of a proof of concept) but it's working, and at least it allows us to scale up if needed. Did you get any outcome at the session by the way ?

@derekcollison
Copy link
Member

You can multiplex the ws connections through one or a small group on NATS TCP connections.

We did discuss and it will be added to the roadmap. Timing is unclear since we have higher priority items that are above this.

@lakedxta
Copy link

github.com/Terry-Mao/goim Supports multiple protocols (WebSocket,TCP,HTTP)

@pires
Copy link
Contributor

pires commented Aug 18, 2016

A rough sketch on my take on this (without changing NATS):

  • Implement server that exposes STOMP (on top of WebSockets)
  • On boot, server connects to at least one NATS instance (from gnatsd 0.9.2 you'll even get a list of other servers, if any, on the cluster, on NATS handshake)
  • Client uses STOMP and servers translates that into NATS protocol, e.g. (UN)SUBSCRIBE

Why STOMP?

  • Asynchronous pubsub alike
  • While the name states it's text-based, it can also transport binary payloads, much like NATS.
  • It's been around for a few years, so one can assume it's mature.

@derekcollison
Copy link
Member

Some good thoughts, we are still looking at providing the Node.js like API over websockets.

@joeblew99
Copy link

Also there is this: https://github.com/sohlich/nats-proxy

@joeblew99
Copy link

@derekcollison
If your thinking about a nodeJS client, then you might also want to think about writing it in Dart.
Why ?

  • Dart compiles to JS, so it will work with nodejs. there are some slight tweaks for IO, but there is a dart lib that supports nodejs.
  • You get a Dart client. That means you support web clients, mobile clients (flutter), and angular-dart suooported. Also it means that server side dart apps can also talk to nats.

The Dart implementation of websockets is 100% independent of web browser btw. SO it does work everywhere.

I plan to use golang and dart in the my microservices sitting behind NATS.

@mcqueary mcqueary changed the title Websocket strategy Support WebSocket Connectivity Mar 7, 2017
@mcqueary mcqueary added this to the Needs Discussion milestone Mar 7, 2017
@sfrooster
Copy link

I'm a little confused by this issue. Is it specifically to support web socket connections into NATS, ie not the behavior of some web socket libraries which degrade to long polling when web sockets are not available?

In the interim, spin-up a node server using socket.io and the node client (and TypeScript, not Dart), imo.

@mcqueary
Copy link

@sfrooster Michael, I believe OP was just wondering what our plans are in general. We have had some internal discussions about supporting NATS over a WebSocket transport in past, but have not taken it much beyond conversation. I'd say we're open to discussion/proposals.

@mcqueary mcqueary added pending and removed pending labels Mar 21, 2017
@1N50MN14
Copy link

+1

Long polling is very expensive specially on mobile devices, combined with the new proposed client auth we'd be able to allow clients (mobile, browser etc) to auth and connect directly to the cluster without having to go through a proxy that manages subscriptions.

@1N50MN14
Copy link

One one more point, adding websocket support to the node.js client is pretty straightforward https://github.com/nats-io/node-nats/blob/master/lib/nats.js#L556 - here we could perform a simple check on the connection url based on which decide whether to establish a tcp connection or a ws connection. Everything else should work out of the box.

@mcqueary mcqueary removed this from the Needs Discussion milestone Mar 24, 2017
@dbskccc
Copy link

dbskccc commented Mar 26, 2017

now i use https://github.com/joshglendenning/websocket-nats and https://github.com/joshglendenning/ws-tcp-relay in browser,it works, but if would be more good for nats server to support it natively.

@cdevienne
Copy link
Contributor

Not a native support but a more natural one than nats-proxy and ws-tcp-relay: https://github.com/orus-io/nats-websocket-gw

@ColinSullivan1
Copy link
Member

Some additional comments from users in another issue:

@isobit commented on May 14
If you're okay with third-party support, you could check out https://github.com/isobit/websocket-nats

@cdevienne commented on May 17
Also, I released https://github.com/orus-io/nats-websocket-gw today.

@1N50MN14
Copy link

@derekcollison Still no plans to add native support for websockets? I would assume people would ask for it even for NATS Cloud (including myself)

@derekcollison
Copy link
Member

We are considering it..

@1N50MN14
Copy link

Well, you have a customer right here, with a live consumer app. Happy to show you and explain my use case.

@1N50MN14
Copy link

1N50MN14 commented Nov 14, 2017

If you're willing to spare few minutes with me that is

  1. I'm a "fan" and an early adopter, been following gnats for 3 years now
  2. I can make my case, from different perspective, and show you why this is valuable to our product which I think will also explain why others are asking for this feature

;)

@slan-ning
Copy link

need native support +1

@ashneverdawn
Copy link

Any update on websocket support?

PaaS providers don't support tcp servers but they do support websockets. So I'm using the websocket transport as a way around this. (+Bonus points for browsers being able to connect)

I'm not going to wait around until nats supports websockets, so in the meantime I'll have to fork it to add the support myself.

@derekcollison
Copy link
Member

Still on our list but other items ahead of it, so feel free to implement a solution that works for you.

@ashneverdawn
Copy link

Implementing websockets ended up being easier than I thought.

The client library .../nats-io/go-nats didn't require any changes, since it supports supplying a custom dialer.

And for the server library .../nats-io/gnatsd I was able to get working with minimal changes by creating a small wrapper library around .../gorilla/websocket to make the websocket.Conn satisfy io.Reader and io.Writer and used it in place of net.Conn. It's mostly a temporary solution until websockets are implemented officially, but it works and serves as a proof of concept and allows me to test what I'm working on.

I now have it properly running on heroku using websockets.
If anyone would like some help setting up an environment like this, let me know. I'd be glad to help.

@cdevienne
Copy link
Contributor

@noxyal nice!

I am curious: does the websocket-enabled go-nats works with https://github.com/orus-io/nats-websocket-gw as a server ?

@ashneverdawn
Copy link

ashneverdawn commented Feb 10, 2018

@cdevienne
I haven't tried with https://github.com/orus-io/nats-websocket-gw but I don't see why not.
go-nats supports specifying a custom Dialer, so you should be able to get it to work with anything, really.

You can check the custom dialer I used here:
https://github.com/noxyal/WSwrapper/blob/master/WSwrapper.go

Then In my code I set it as the custom dialer like this:

natsOptDialer := nats.SetCustomDialer(&WSwrapper.Dialer{})
natsConn, err := nats.Connect(natsURL, natsOptDialer)

@dbskccc
Copy link

dbskccc commented Feb 6, 2019

the blog(https://nats.io/blog/2018-year-review-upcoming-2019/) shows native websocket support will land in 2019, does it has a timeline yet?

@derekcollison
Copy link
Member

1H 2019

@liqweed
Copy link

liqweed commented Mar 19, 2019

Would this apply to nats-streaming as well?

@derekcollison
Copy link
Member

Yes since a connection to STAN is logical..

@blinkinglight
Copy link

in case someone is still looking nats client or nats-streaming client via websockets https://github.com/blinkinglight/go-nats.io-websocket

@derekcollison
Copy link
Member

Thanks will have @aricart take a look. /cc @ColinSullivan1

@paravz
Copy link

paravz commented Jun 24, 2019

Would this WebSocket work include "TLS-only" mode for server and clients, removing the initial plaintext INFO message ? This is regarding discussion in #291

@derekcollison
Copy link
Member

Most likely we would make the WS connections be able to traverse through proxies and LBs etc that supported TLS. Meaning yes would be a TLS-only, however there is no plan to remove that for normal connections.

@paravz
Copy link

paravz commented Jun 24, 2019

I created #1044 as a point to discuss such option and if it makes sense work on PR to enable it

@derekcollison
Copy link
Member

I saw, websockets will support this mode, but there are no plans to change the way connections work today with sending INFO first unencrypted before switching to TLS.

@michaellee8
Copy link

Will the websocket connectivity be supported so that this can replace rabbitmq?

@derekcollison
Copy link
Member

Yes, we have plans to support websockets post JetStream launch.

@derekcollison
Copy link
Member

This is supported now on master.

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