Edit Page

Realtime communication between the client and the server

The easiest way to send a realtime message from a client to a Sails app is by using the sails.io.js library. This library allows you to easily connect sockets to a running Sails app, and provides methods for making requests to Sails routes that are handled in the same manner as a "regular" HTTP request.

The sails.io.js library is automatically added to the default layout template of new Sails apps using a <script> tag. When a web page loads the sails.io.js script, it attempts to create a new client socket and connect it to the Sails app, exposing it as the global variable io.socket.

Examples

Include the sails.io.js library, and make a request to the /hello route of a Sails app using the automatically-connected socket:

<script type="text/javascript" src="/js/dependencies/sails.io.js"></script>
<script type="text/javascript">
io.socket.get('/hello', function responseFromServer (body, response) {
  console.log("The server responded with status " + response.statusCode + " and said: ", body);
});
</script>

Now consider this more advanced (and much rarer) use case: Let's disable the eager (auto-connecting) socket, and instead create a new client socket manually. When it successfully connects to the server, we'll make it log a message:

<script type="text/javascript" src="/js/dependencies/sails.io.js" autoConnect="false"></script>
<script type="text/javascript">
var mySocket = io.sails.connect();
mySocket.on('connect', function onConnect () {
  console.log("Socket connected!");
});
</script>

Socket requests vs traditional AJAX requests

You may have noticed that a client socket .get() is very similar to making an AJAX request, for example by using jQuery's $.get() method. This is intentional—the goal is for you to be able to get the same response from Sails no matter where the request originated from. The benefit to making the request using a client socket is that the controller action in your Sails app will have access to the socket which made the request, allowing it to subscribe that socket to realtime notifications (see sending realtime messages from the server).

Reference

  • View the full sails.io.js library reference.
  • 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.