Back to Sails

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

docs/concepts/Realtime/Multi-server environments.md

12.12.20002.9 KB
Original Source

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

  • See the full reference for the sails.io.js library to learn how to use sockets on the client side to communicate with your Sails app.
  • See the sails.sockets reference to learn how to send messages from the server to connected sockets
  • See the resourceful pub-sub reference to learn how to use Sails blueprints to automatically send realtime messages about changes to your models.
  • Visit the Socket.io website to learn more about the underlying library Sails uses for realtime communication
<docmeta name="displayName" value="Multi-server environments">