concept event in category sails

This is an excerpt from Manning's book Sails.js in Action.
You might wonder why you add the io.socket.on() method
. Earlier, we explained that when io.socket.get('/video') triggered the blueprint find action, that action added the requesting browser’s socket to a list. Whenever a video record is changed or a new video record is created, that list is notified of the event. The io.socket.on() method listens for the event and, if triggered, updates the video array on $scope
. Because Sails socket requests are not part of Angular, you need to use $scope.apply()
in order for the digest cycle to kick in and update the DOM. You’re now equipped to listen for changes to the video model and update the DOM accordingly. To complete the process, you need to update the Add Video request to Sails WebSockets.
Unlike HTTP, once a client connects to the WebSocket server and establishes a WebSocket id, the server can send events to listening clients with a matching id and vice versa. This is in contrast to HTTP, where the client must first make a request, which briefly opens a connection, before the server can respond and close it. Once the server responds, the connection is closed and the server can no longer initiate communication with the client until another request is received. Figure 14.1 illustrates this important difference between HTTP and WebSockets communication.
The Sails WebSocket client is a lightweight wrapper that sits on top of the WebSocket.io client, whose purpose is to simplify the sending and receiving of events from your Sails backend. You’ve connected to the WebSocket server via the client throughout the book. For example, restart Sails using sails lift and navigate your browser to localhost:1337 with the browser’s console window open. You should see a browser console log message indicating that a connection was established, as shown in figure 14.2.
Figure 14.2. Each time Sails responds with a server-rendered view, a new WebSocket connection is created.
![]()
This message is coming from the Sails WebSocket client and is automatic as long as you utilize the default sails.io.js. Now that you’ve established a connection between the client and the Sails WebSocket, you can build requests on the frontend and add methods to controller actions on the backend. This allows you to create and join rooms, send events to connected WebSockets, and more.
Example 14.15. Adding the event handlers for typing and stoppedTyping events
... io.socket.on('typing', function (e) { console.log('typing!', e); $scope.usernameTyping = e.username; $scope.typing = true; $scope.$apply(); }); io.socket.on('stoppedTyping', function (e) { console.log('stoppedTyping!', e); $scope.typing = false; $scope.$apply(); }); ...
. Earlier, we explained that when io.socket.get('/video') triggered the blueprint find action, that action added the requesting browser’s socket to a list. Whenever a video record is changed or a new video record is created, that list is notified of the event. The io.socket.on() method listens for the event and, if triggered, updates the video array on $scope
. Because Sails socket requests are not part of Angular, you need to use $scope.apply()
in order for the digest cycle to kick in and update the DOM. You’re now equipped to listen for changes to the video model and update the DOM accordingly. To complete the process, you need to update the Add Video request to Sails WebSockets.