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 socket.io-redis as a dependency of your Sails app.
  3. Updating your sails.config.sockets.adapter setting to 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 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

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Concepts

  • Assets
    • Default Tasks
    • Disabling Grunt
    • Task Automation
  • Blueprints
    • Blueprint Actions
    • Blueprint Routes
  • Configuration
    • The local.js file
    • Using `.sailsrc` Files
  • Controllers
    • Generating Controllers
    • Routing to Controllers
  • Custom Responses
    • Adding a Custom Response
    • Default Responses
  • Deployment
    • FAQ
    • Hosting
    • Scaling
  • Extending Sails
    • Adapters
      • Available Adapters
      • Custom Adapters
    • Generators
      • Available Generators
      • Custom Generators
    • Hooks
      • Hook Specification
        • .configure()
        • .defaults
        • .initialize()
        • .routes
      • Installable Hooks
      • Project Hooks
      • Using Hooks
  • File Uploads
    • Uploading to GridFS
    • Uploading to S3
  • Globals
    • Disabling Globals
  • Internationalization
    • Locales
    • Translating Dynamic Content
  • Logging
    • Custom log messages
  • Middleware
    • Conventional Defaults
  • Models and ORM
    • Associations
      • Dominance
      • Many-to-Many
      • One Way Association
      • One-to-Many
      • One-to-One
      • Through Associations
    • Attributes
    • Lifecycle callbacks
    • Model Settings
    • Models
    • Query Language
    • Validations
  • Policies
    • Sails + Passport
  • Programmatic Usage
    • Tips and Tricks
  • Realtime
    • Multi-server environments
    • On the client
    • On the server
  • Routes
    • Custom Routes
    • URL Slugs
  • Security
    • Clickjacking
    • Content Security Policy
    • CORS
    • CSRF
    • DDOS
    • P3P
    • Socket Hijacking
    • Strict Transport Security
    • XSS
  • Services
    • Creating a Service
  • Sessions
  • Testing
  • Views
    • Layouts
    • Locals
    • Partials
    • View Engines

Built with Love

The Sails framework is maintained by a web & mobile studio in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Treeline IDE
  • Contribute
  • Logos/artwork
About:
  • The Sails Company
  • Security
  • News
  • Legal
Help:
  • Get started
  • Documentation
  • Docs
  • Enterprise
  • Hire us

© 2012-2018 The Sails Company. 
The Sails framework is free and open-source under the MIT License.