scttnlsn / backbone.io

Backbone.js sync via Socket.IO

Home Page:http://scttnlsn.github.io/backbone.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Channels/rooms support

podviaznikov opened this issue · comments

I have following case:
I want models synchronized not between all clients but between few selected ones.

I did following changes in the library:
browsers.js

buildBackedn method

backend.socket.on('connect', function () {
backend.socket.emit('set channel', 'SOME_CHANNEL_ID');
});

index.js

on 'connection' listener

socket.on('set channel', function(channel) {
socket.join(channel);
});

on 'sync' listener

socket.get('channel', function (err, channel) {
socket.broadcast.to(channel).emit('synced', req.method, result);
});

It works for me. However I have 2 questions:

  1. Is there is another way to achieve following functionality?
  2. Can we add some optional support for such functionality inside the library?

This seems like a really nice way to handle "nested collections" as well. Perhaps instead of an explicit "set channel" step we could just send the (optional) channel along with the "listen" event? Also, rather than polluting the collection object with more properties we could allow the backend to be specified like:

backend: { name: 'foo', channel: 'bar' }

or still just

backend: 'foo' if no channel is desired.

Thoughts?

I implemented this in my fork. Can send pull request.

Like the idea with one backend object:
backend: { name: 'foo', channel: 'bar' }

@podviaznikov I took a look at your fork and implemented some of the alterations that I previously mentioned. 1e6d820 Let me know if that works like you expect and I can merge it into master.

Hi, your changes are ok with me. I tested and seems that everything works.