Edit Page

Realtime communication in a multi-server (aka "clustered") environment

With the default configuration, Sails allows realtime communication between a single server and all of its connected clients. When scaling your Sails app to multiple servers, some extra setup is necessary in order for realtime messages to be reliably delivered to clients regardless of which server they’re connected to. This setup typically involves:

  1. Setting up a hosted instance of Redis.
  2. Installing @sailshq/socket.io-redis as a dependency of your Sails app.
  3. Updating your sails.config.sockets.adapter setting to @sailshq/socket.io-redis and setting the appropriate host, password, etc. fields to point to your hosted Redis instance.

No special setup is necessary in your hosted Redis install; just plug the appropriate host address and credentials into your /config/sockets.js file and the @sailshq/socket.io-redis adapter will take care of everything for you.

Note: When operating in a multi-server environment, some socket methods without callbacks are volatile, meaning that they take an indeterminate amount of time to complete, even if the code appears to execute immediately. It's good to keep this in mind when considering code that would, for example, follow a call to .addRoomMembersToRoom() immediately with a call to .broadcast(). In such cases, the new room member probably won't receive the newly broadcasted message, since it is unlikely that the updated room membership had already been propagated to the other servers in the cluster when .broadcast() was called.

Reference

#

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts