Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
The previous ( legacy ) version of AdonisJs has support for Web-sockets, which has been removed from AdonisJs 4.0 and not something anyone was expecting. However, the implementation was insufficient and had some potential issues with horizontal scaling.
Also, it was implemented on top of socket.io, where some users did complain about memory leaks. I personally never faced any issues with
Nevertheless, I wanted to set some clear and realistic goals, which can help in making AdonisJs the perfect fit for real-time apps.
The big concerns in scaling web-sockets are
Stateful, but starts stateless
If you have any prior experience with Web-sockets, you do know that majority of frameworks ( including socket.io ) starts an HTTP connection with long polling, which later gets upgraded to a web-socket connection.
Now when these HTTP calls are being made, we need to make sure they reach to a single server. For that we have to implement sticky sessions on a load balancer, which makes sure that a request with
This was required because there wasn't proper browser support for Web-sockets and some private networks kill long-lived web-socket connections.
As far as browsers are concerned, all major browsers support web-sockets http://caniuse.com/#search=WebSocket and there are simple ways to tackle with long-lived connections killing.
The implementation of AdonisJs will use https://github.com/websockets/ws, which makes a pure Web-socket connection and thus there is no need of implementing sticky sessions.
If your application is running on multiple servers, then you need a way of notifying users connected to these servers. For example
One classic way to solve this problem is using the
Wait wait wait! If Redis goes down, for sure your application also goes down with it. I will share the Adonis implementation in a while after the next section.
Delivering messages via Pub/Sub works excellent with Redis, but how about sharing state. For example:
We want to show a list of all the online users across multiple servers and notifying all servers when a new user connects/disconnects.
The naive way is to store this info inside Redis again ( since we are using it for pub/sub ) with a list of users for a given server. Now if a server dies, we will have orphan data in Redis living forever.
I have spent a good chunk of time building small demo’s to see what can be an ideal way to combat these issues and the answer is to remove the external dependency on Redis and instead make all servers talk to each other directly.
I am working on a small module which let you connect to servers inside the same network or across data centers using TCP. Each server will act as a server + client at the same time and uses the Gossip protocol to find when a server dies unexpectedly.
This is how it will look eventually.
Instead of using Redis for pub/sub, Adonis will use the
If a server dies gracefully, it will notify all connected clients, and in case of unexpected failure, the
It helps in
When can I use it?
I will start working on it from ( 11th November, 2017 ), and hence the entire module will take about a month and a half to get completed.
Once again, If you want to help, please consider donating on Patreon so that eventually I can spend more time on the framework.
It still doesn't work ... even following the cluster mode, making it compatible with PM2, adonis JS still can't send WebSocket notifications to multiple ports in a cluster mode.
I tried using the PM2 cluster mode and tenten doing the load balance and with multi-fork it didn't even work ...
I read about:
and so far the only hope of making it work is with Redis even though it’s not ideal.
Does anyone know if adonisJS keeps trying to be compatible with cluster or multi-server mode? because what I see for 3 years now has topics about it without any with a clearer resolution on the subject ..