F
F
friktor2014-04-11 19:53:31
Node.js
friktor, 2014-04-11 19:53:31

How to set Socket.IO handlers in SailsJS on client side in DOM?

After I decided to write a simple to-do list using sockets, I got into the off-docs, from the docks I realized that the binding action occurs according to the principle: a handler is placed in the controller that notifies of all changes in the Model, and it is called subscribe .

Further, on the client side in JS, you can use the functions of the basic suckat socket.io , as well as additional gingerbread of Sails itself, which are described on the same page of the docks, a little resting on the use of this functionality, I looked at stackoverflow - in which I was told to look at the cast on using sockets .

I caught the main point and was able to get my controller to follow the updates using the method specified in the cast

subscribe: function (req, res) {
    Task.find(function foundTask (err, tasks) {
      if (err) return next(err);

      // оповещает когда создается запись
      Task.subscribe(req.socket);
      // оповещает когда произошло обновление или удаление
      Task.subscribe(req.socket, tasks);
    });
  }

But I just can't understand how on the client side you can create a handler that could update the list?

I realized that it all comes down to the fact that when updating, you need to make sure that this update is transferred as a list to the DOM using even banal jquery, but I don’t know the approximate algorithm of actions used for such manipulations.

If you can, tell me the algorithm, article, docks, or code example for this task.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2014-10-12
@Ayahuaska

subscribe subscribes you to certain events. If I understand everything correctly, then in the controller methods that change the model, you need to select the publish methods, for example, publishCreate.
Then all subscribed clients will receive the message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question