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 sails.js 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: myCollection = App.subscribe('myCollection') bob = myCollection.find({name: 'Bob'}) myCollection.insert({name: 'Amelie'}, callback)

All interaction with the server should happen in the background.

-------------Problems Reply------------

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.


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

  • In meteor, the client can subscribes to everything he wants, and the server control what it receives by only publishing to who he wants;
  • whereas in sails.js, the server both does subscribe some clients sockets and publish to all binded sockets

Note that, by default:

  • meteor contains the autopublish package that just notify every client without any kind of filtering. To acheive some filtering, you have to meteor remove autopublish then you can handle what will your client receive by adding a mongo request to it, like explained here.
  • sails by default, on its automatic "select" blueprints actions, auto-subscribes the calling socket to the events on the objects returned by the "select".

As a server-side conclusion:

  • Subscribe: just call findor findOne blueprint default action, through a socket (attaching some where filters or not) and your socket will automatically be subscribed to every events concerning returned objects => you don't have to code anything on the server, in most cases, for the Subscribe logic.
  • Publish: every blueprint default actions (create, update, destroy, add, remove) auto-publish to subscribed sockets => you don't have to code anything on the server, in most cases, for the Publish logic.

(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.


  • meteor in one hand, with it's isomorphic dimension, does provide a front-end connector by nature, exposing it's data-bound collections.
  • sails on the other hand, is front-end agnostic, and can be attacked by any http REST connector (JS or not), such as $http, $resource, or more advanced ones like Restangular.
    Though, being aware of the complexity using raw sockets on their API (when it comes to session, CORS, CSRF and stuff), they developped a javascript wrapper called designed to be REST-like-over-socket, and just works like a charm.

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., 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), or even raw 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:


  • Meteor

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

  • Sails

    //Nothing to do, just sails generate api myCollection


  • Meteor

    # Client-side:
    myCollection = App.subscribe('myCollection')

  • Sails, with (Here using lodash for convenience)

    var myCollection;'/myCollection').then(
    function(res) {
    myCollection =;
    function(err) {
    //Handle error
    );'myCollection').function(msg) {
    switch(msg.verb) {
    case 'created':
    case 'updated':
    _.extend(_.find(myCollection, 'id',,;
    case 'destroyed':
    _.remove(myCollection, 'id',;

    (I leave the find where and create to your imagination with [the doc])

All interaction with the server should happen in the background.

  • Well, Sails, only for angular, with sails ressources

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.

Category:javascript Views:1 Time:2018-03-11

Related post

  • client-side text-box character limit validation using jQuery? 2009-08-20

    I have a textbox that must have a MIN number of characters as well as a MAX number of characters. What is the best way, using jQuery, to make sure that the input is within the range (while displaying status messages to the user) before allowing the u

  • Using Client-Side script to evaluate month difference using PeterBlum DifferenceValidator 2009-10-19

    Problem: I am using Peter Blum's Professional validation controls ( throughout my project and have come across a validation that should be done on the client side rather than going back to the server.

  • How to do client side paging on a DevExpress GridView using a List DataSoure dynamically? 2011-11-23

    Using DevExpress v11.1 I've implemented a List based datasource bound to the gridview dynamically in code behind. Attached to this I use the client side PerformCallback to populate the grid with data based on user selections. This all works fine. How

  • Server and client side programming language and frameworks to use in embedded systems 2012-01-09

    I am going to implement an easy-to-use and eye-catching graphical user interface for management and configuration of an ethernet switch which has a flash (of just 16 MB) mounted in it, which will have an embedded linux, some specialized softwares, CL

  • Using Socket.IO, to prevent client-side hijacking when sending JavaScript events - use SignalR? 2012-03-05

    Using Socket.IO, clients are notified when other clients perform certain activities, through a JavaScript call to the Socket.IO server. That message is broadcast to all clients, and client-side logic decides who sees the notification. (Another option

  • What is client-side routing and how is it used? 2012-04-17

    I was learning a little bit about KnockoutJS and got to this question, which was really informative. I found something I'm not aware of or didn't understand - this answer talks about "client-side URL routing". I continued exploring about the issue an

  • Client side validation of from which is using partial views 2013-05-21

    I just completed the visualization logic for my form and now I want to get use of the client side validation that mvc 3 provides. However even though I'm following some examples I can't make it work and I don't know what might be the reason.

  • Can an android application(client) communicate with a java web server using sockets? 2011-12-23

    I need an android application to communicate with a java web server using sockets, but can't seem to find any information regarding this. I have only been able to find information about java to java and android to android. --------------Solutions----

  • Swapping Out Client-Side Sync Databases 2011-08-02

    We sort of shot ourselves in the foot at a client site with regards to the Microsoft Sync Framework when troubleshooting a particular production issue... We have a central server as the data system of record which exposes a WCF service over which we

  • Keeping android client in sync with server 2012-02-19

    We have an android app which allows a user to download data from a GAE back end and also send data changes/user input back to the GAE. All data from the server is also copied to the local DB on the android client. What we are trying to figure out is

  • Using Google channel api on the client side by embedding javascript in java using rhino or standard inbuilt javascript support? 2011-11-14

    I am just experimenting about and I want to know whether it will be possible (and if possible how should I go about it?) to create a desktop client which will use javascript embedded in java using rhino or simply the inbuilt libraries(which are rhino

  • Client Side validation on Backbone.js collection 2012-02-23

    I'm using backbone-on-rails gem and got the following: Model class Crayons.Models.Color extends Backbone.Model validate: (attrs) -> if attrs.colorname.length == 0 return "colorname can't be empty" Collection class Crayons.Collections.Color extends

  • Input on how to keep object map in sync between browser (javascript) and server ( web service/database)? 2009-06-19

    I have a object map on my server (objects representing Customers, Orders, Items, related to each other, based on content from a database) and want to keep an up to date version of that object map in the browser where the user is manipulating and view

  • How to prevent PostBack on the client side? 2010-06-11

    I have some validation JS code on client, that must be executed befor PostBack. If this validation code return 'false', postback is needless. How it can be disabled? --------------Solutions------------- Remember that the real validation should always

  • Client-side logic OR Server-side logic? 2009-10-04

    I've done some web-based projects, and most of the difficulties I've met with (questions, confusions) could be figured out with help. But I still have an important question, even after asking some experienced developers: When functionality can be imp

  • Client-side image processing 2010-02-01

    We're building a web-based application that requires heavy image processing. We'd like this processing load to be on the client as much as possible and we'd like to support as much platforms (even mobiles) as much as possible. Yeah, I know, wishful t

  • How to get child controls correct id to client side 2010-02-01

    I am working on ASP.NET and not using any ASP.NET's AJAX framework. Now I am trying to update the contents of the textboxes and dropdowns in the Grid controls cell on client side using (classic JavaScript way)AJAX. But problem I am facing is that con

  • How to show huge amount of report data client side (mongoDB, JS) 2011-02-05

    Due to the nature of the data that we collect in our application, its necessary to allow user to be able to search through 1,000,000+ rows of data, and receive results client side as fast as possible. We have played around with various techniques, an

  • What are the valid use cases for client side encryption? 2011-08-19

    I just read about the Stanford Javascript Crypto Library (jsfiddle example) which supports SHA256, AES, and other standard encryption schemes entirely in javascript. The library seems very nifty, but I don't know of a reasonable use case for it. As s

  • Script or tool for client-side logging 2011-09-12

    Is there any complete solution for client-side logging? I need this tool or script to collect information about user actions (interaction with controls as clicks, form submits, using inputs...) and send it to the server. Our system is closed web-base

  • How to Save a file at client side using JavaScript? 2011-11-10

    I want to save a JSON object to a file at client to persist it for future usage, I have tried following line"data:text/json;charset=utf-8," + escape(JSON.stringify(obj))); and it works!! Problem it asks user the location and name of the

  • Server-side and client-side JavaScript 2011-12-12

    I am having great difficulty choosing which frameworks to use for an app that is about to go into development. I am a front-end guy, and my friend is doing the back-end. Say I was developing a simple todo list app. I have a template for each item on

  • Using client side reporting vs. server side reporting? 2008-09-26

    When do we use client side reporting and when do we use server side reporting? Which reporting is best practice (client/server)? This pertains to SSRS reporting. --------------Solutions------------- Well... client side reporting you'd use if you've g

  • Flash upload image resize client side 2009-01-10

    Does anyone got an ideia on how to get client side image resize using flash. Example: Client chooses an image with 1200x800 and before it uploads it flash will turn it into half of it or something. Any thoughts? --------------Solutions------------- P

  • PHP Secure Login - Client-side options? 2009-07-23

    Ok now I am confused. I have been getting advice from SO users on libraries to use with codeigniter for authentication. I have explored dx_auth and simpleloginsecure (I think I will use the latter due to its supposed secure hashing and small footprin

Copyright (C), All Rights Reserved.

processed in 0.291 (s). 11 q(s)