javascriptnode.jssocket.iosails.jssails.io.js

Keeping a client-side sync of Sails.js collection, using sockets


I very much like Meteor's pub/sub. I wonder if there is a way to get a similar workflow, using or just a socket library in general.

In particular, what I would like to be able to do is something along the lines of:

// Server-side:
App.publish('myCollection', -> collection.find({}))

// Client-side:
let myCollection = App.subscribe('myCollection')
let bob = myCollection.find({name: 'Bob'})
myCollection.insert({name: 'Amelie'}, callback)

All interaction with the server should happen in the background.


Solution

  • I very much like Meteor's pub/sub. I wonder if there is a way to get a similar workflow, using sails.js or just a socket library in general

    Basically yes, at least about realtime sync between backend and frontend. Let's review what meteor's have and answer point by point.

    Pub/sub

    The Pub / Sub concept, as stated by Sabbir, is also supported by sails.js. Though the basics are slightly different :

    Note that, by default:

    As a server-side conclusion:

    (Though, if you find yourself implementing some manual controller actions, sails API helps you publishing and subscribing easily)

    Client handling

    Therefore, with both meteor and sails, clients only receive what they're supposed to receive. Time for front-end now.

    Philosophy

    Basically, The main difference is that meteor is one step higher-level than sails, because it provides the logic of syncing collections and objects.

    All interaction with the server should happen in the background.

    sails.io.js, the official front-end component, is just not that high-level. When it comes to Angular.js.

    Though, you can find some community connectors that aim to, kinda, provide the same feature as mongo data-bound collections and objects. There is sails-resource, spinnaker or angular resource sails. I tried both of them, and I should say that I was disapointed. The abstraction level is so high that it just becomes annoying, IMHO. For example, with not-very-RESTful-friendly custom actions, like a login, it becomes very hard to adapt it for your needs.

    ==> I would advice to use a low-level connector, such as angularSails or (my prefered) https://github.com/janpantel/angular-sails, or even raw sails.io.js if you're not using Angular.

    Edit: just foun a backbone version, by the sails' creator

    It just works great, and believe me, the "keep my collection in sync with that socket" code is so ridiculous, that finding a module for this is just not worth it.

    Some code please, stop talking

    In particular, what I would like to be able to do is something along the lines of:

    Server Client

    All interaction with the server should happen in the background.

    I'm not pretty used to that process, so I leave you reading here or here, but once again I'd choose manual .on()method.