Scalable Websockets - I am confused

时间:2016-03-04 18:17:53

标签: angularjs laravel websocket

I've been googling it, and I'm a little confused about this. Pretty much everything I've read about it has 1 use-case but it doesn't seem like the best to me. Maybe it is though? Not sure.

Let me set the environment first: The frontend is powered by AngularJS An two backend APIs powered by Laravel (we'll call them A & B).

Now, all 3 of those scale independently of each other. And each one is in it's own docker container. There are load-balancers in front of each thing that handles routing and everything. I am using RDS for my database.

Now, the way I normally see, is to use a hash-ip thing for the loadbalancer, so that you just stay connected to the server that deals with whatever websocket events you need.

Well, that doesn't really seem like what I want. Really, long-polling seems like what I want, but I thought maybe I could get websockets to work. Basically, anytime ANYONE changes anything on either of the APIs, an event is fired. Then I need ALL of the frontends to update. Now as far as I understand, websockets use redis to keep track of events.

If it does use redis, how do you scale redis to be on multiple servers? Or would using something like elasticache be easier and then I just don't have to deal with that part?

Is all of this basically true? Am I on the right track with my thinking?

Thanks for taking a look. Sorry if it's a little ramble'y or confused. I am still not clear on it so everything I wrote is kind of just my thoughts.

1 个答案:

答案 0 :(得分:0)

我明白了。我会为碰巧遇到这种情况的其他人发布答案。

所有这些都像我需要的那样独立扩展。但这有点令人困惑,而且我使用的是Docker,所以我的回答也会涉及到这一点。

所以在Laravel上,我只是设置事件。他们将被发送到Redis Labs上的redis服务器。

在我的前端docker上,有一个AngularJS应用程序,websocket.js服务器也通过端口3000上的节点运行。在容器上,有Nginx为它们服务,AngularJS交易只是一个普通的服务器指令。节点的一个是代理传递目录,因此端口80正常工作,但它被转发到后端的端口3000.

websocket.js服务器端部件,连接到Redis Labs上的同一台redis服务器。

AngularJS应用程序是使用websockets客户端代码的应用程序。

现在还有另一个运行nginx服务的docker容器进行负载均衡。因此,loadbalancer是所有websockets,api和前端URL都经过的。

将websocket.js放入其自己的容器并独立扩展可能会更好。但是,现在,我认为将前端和websocket.js一起扩展可以很好地工作。

这是我如何做到这一点的概述。我也从这里使用了很多东西:

https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51

https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/