Can you tell me how horizontal scaling would work, specifically for web sockets? I’m aware that long-lived connections can be difficult to load balance, so was wondering whether App Platform can handle that out of the box or whether we’d need to do something clever ourselves.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
While App Platform supports websockets, the implementation of how this communication works across horizontally scaled instances will require different behaviors based on that app requirements. App Platform provide this for you.
The key is to have some sort of correlation strategy built into your app. This might be a database such as Postgres or MySQL, or an in-memory datastore like Redis.
An example of this is a web chat system with multiple users and multiple instances using Redis pub/sub channels for correlation. When a user sends a message that message will be pushed onto a Redis pub/sub channel where all of the instances of your app are listening. The instances will push the message to the users who are connected to them via websockets.
To paraphrase, the Redis pub/sub channels act as the intermediary so it doesn’t matter which instance your user is connected to, they will messages sent from users on other instances.
Just to add to this. We have been playing with the app platform with our socket.io based Node app and it seems to be working well. Our setup, as mentioned above is using socket.io-redis
This does as described above but takes some of the complexity out of it by abstracting the redis pub / sub portion
The key with your web sockets is to have the pub / sub maintain a listing of where each socket is connected and to run on / emits on the correct server.
Some things to be aware of though is that redis pub / sub can store some items but not all when it comes to say the full socket handshake etc. This means that custom things outside the socket like say a ‘nickname’ or other properties that may be assigned to a socket will not be readily available so you will have to store these on your own in redis or mongo etc.