How do I subscribe to a model instance in Sails.JS?

后端 未结 1 436
误落风尘
误落风尘 2020-12-24 08:38

I am attempting to use the subscribe function described here. However, when editing /assets/js/app.js, I am getting this error:

Uncaught Referen         


        
相关标签:
1条回答
  • 2020-12-24 09:27

    To subscribe to a model instance, I use the following Real-Time Model Event pattern, some of which resides on the client and some on the server. Keep in mind the client can’t just subscribe itself- you have to send a request to the server letting it know that you’d like to be subscribed-- this is the only way to do it securely. (e.g. you might want to publish notifications with sensitive information-- you want to make sure a connected socket has permission to see that information before subscribing them to it.)

    I’m going to use an example of an app with a User model. Let’s say I want to notify folks when existing users login.

    Client-Side (Part I)

    On the client-side, for simplicity, I’m going to use the existing app.js file in the /assets/js folder (or /assets/linker/js folder if you used the --linker switch when you built the app.)

    To send my socket request to the server within assets/js/app.js, I’m going to use the socket.get() method. This method mimics the functionality of an AJAX “get” request (i.e. $.get() ) but uses sockets instead of HTTP. (FYI: You also have access to socket.post(), socket.put(), and socket.delete()).

    The code would look something like this:

     
    // Client-side (assets/js/app.js)
    // This will run the `welcome()` action in `UserController.js` on the server-side.
    
    //...
    
    socket.on('connect', function socketConnected() {
    
      console.log("This is from the connect: ", this.socket.sessionid);
    
      socket.get(‘/user/welcome’, function gotResponse () {
        // we don’t really care about the response
      });
    
    //...
    

    Server-Side (Part I)

    Over in the welcome() action in UserController.js, now we can actually subscribe this client (socket) to notifications using the User.subcribe() method.

     
    // api/UserController.js
    
    //...
      welcome: function (req, res) {
        // Get all of the users
        User.find().exec(function (err, users) {
          // Subscribe the requesting socket (e.g. req.socket) to all users (e.g. users)
          User.subscribe(req.socket, users);
        });
      }
    
    //...
    

    Back on the client-side (Part II)...

    I want the socket to ‘listen’ for messages I’m going to send it from the server. To do this I’ll use:

     
    // Client-side (assets/js/app.js)
    // This will run the `welcome()` action in `UserController.js` on the backend.
    
    //...
    
    socket.on('connect', function socketConnected() {
    
      console.log("This is from the connect: ", this.socket.sessionid);
    
      socket.on('message', function notificationReceivedFromServer ( message ) {
        // e.g. message ===
        // {
        //   data: { name: ‘Roger Rabbit’},
        //   id: 13,
        //   verb: ‘update’
        // }
      });
    
      socket.get(‘/user/welcome’, function gotResponse () {
        // we don’t really care about the response
      });
    
    // ...
    

    Back on the server-side (Part II)...

    Finally, I’ll start sending out messages, server-side, by using: User.publishUpdate(id);

     
    // api/SessionController.js
    
    //...
      // User session is created
      create: function(req, res, next) {
    
        User.findOneByEmail(req.param('email'), function foundUser(err, user) {
          if (err) return next(err);
    
          // Authenticate the user using the existing encrypted password...
          // If authenticated log the user in...
    
          // Inform subscribed sockets that this user logged in
          User.publishUpdate(user.id, {
            loggedIn: true,
            id: user.id,
            name: user.name,
            action: ' has logged in.'
          });
        });
      }
    //...
    

    You can also check out Building a Sails Application: Ep21 - Integrating socket.io and sails with custom controller actions using Real Time Model Events for more information.

    0 讨论(0)
提交回复
热议问题