Learn Sails.js - Part 6

von Manuel am 28.09.2025 um 12:00 Uhr

Excellent. The crew is organized, but our communication methods are antiquated. Sending a message to another ship requires a fast sloop and a fair wind. What if we could send messages instantly, as if by magic? It’s time to install the ship’s most advanced piece of equipment.


Part 6: Instant Dispatches (Real-time with WebSockets)


The Captain’s Briefing (Background and Theory)

Navigator, thus far all our communication has followed the “message in a bottle” protocol (standard HTTP). We send a request from our browser, and some time later, the server sends a response back. The server can never speak first. If a new treasure is discovered somewhere in the world, our ship won’t know about it until we manually ask for an update by refreshing the page.

This changes today. We are installing a Magical Telegraph, a technology known in other worlds as WebSockets.

A WebSocket isn’t a single message; it’s an open, persistent, two-way communications channel between the browser and your Sails server. It’s like having a direct, magical link. Your server can now push information to the browser at any time, without being asked. This is the foundation of all modern real-time features: live chats, notifications, activity feeds, and collaborative applications.

Sails.js and Socket.io: The Telegraph Machine

Setting up WebSockets from scratch is a complex feat of engineering. Thankfully, Sails comes with a masterfully built telegraph machine called Socket.io integrated directly into its core. Sails provides a set of simple, elegant commands to manage this powerful technology.

Subscribing to a Channel (The Room)

The key to managing real-time communication is organization. You don’t want every message going to every person. You create channels, or “rooms.” A socket (a single browser connection) can be made to join a room. sails.sockets.join(req, 'some-room-name'); This command tells Sails, “The browser that made this request is now listening to the ‘some-room-name’ channel.”

Broadcasting a Message

Once sockets have joined a room, you can send a message specifically to that room from your server-side code: sails.sockets.broadcast('some-room-name', 'eventName', { some: 'data' }); This sends a message with the name eventName and a data payload to every single socket listening to that channel.

Blueprint Pub/Sub: The Auto-Magic

Here is where Sails ascends from useful to truly magical. This concept is called Publish/Subscribe, or “Pub/Sub.” When you use the Blueprint API from a browser over a socket connection, Sails automatically manages the room subscriptions for you!

This means you get real-time model updates for free, just by using the API you already know over a socket connection.

Listening on the Client

How does the browser listen for these incoming telegraph messages? Sails provides a client-side JavaScript file, sails.io.js, which is included for you automatically. It gives you a global io object. The most important function is io.socket.on():

// On the front-end, in a <script> tag
io.socket.on('eventName', function (data) {
  // This code runs when a message called 'eventName' arrives
  console.log('The server sent a message!', data);
});

Key Concepts Checklist


Mission Log: Quest - “The Live Treasure Tracker”

We will build a simple dashboard that displays a list of all treasures. Then, using the magic of WebSockets, we will make it so that when a new treasure is created anywhere, it instantly appears on our dashboard page without a refresh.


Mission Debrief (Review & Outcomes)

Incredible! The magical telegraph is online. You have achieved one of the most powerful and sought-after features in modern web development.

You created a page that subscribed itself to model updates just by fetching its initial data over a socket. When another user (you, in another tab) modified the data, the Sails backend published an event, which was instantly pushed to your tracker page. Your client-side JavaScript caught the event and updated the display. This entire complex workflow was handled with just a few lines of code, demonstrating the immense power and productivity of the Sails.js framework.


Rewards & Promotion

As Quartermaster, you are a master of the ship’s stores (Models) and now its communications (Sockets). You hold a position of immense trust and authority. Your next duty is to ensure the security of the ship.