angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

socket.io mixin - OR how to create a mixin which modifies to a collection?

Open facultymatt opened this issue 11 years ago • 3 comments

I am attempting to create a mixin that will do the following to make any model "socket" compatible:

  • Decode and add new item to collection
  • Delete from collection
  • Update from collection
  • Work automatically when when using Trip.find(1) and Trip.$search() and Trip.events.$fetch()

I'm using code that looks something like this:


//socket api sends messages in the following format:
{
    model: "Trip",
    verb: "create",
    data: {tripId: 2, name: 'my epic trip!'}
}

// pseudo code
return restmod.mixin(function() {

  this.define('Scope.$socket', function() {
    var Scope = this;
    var model = Scope.$type;

    var sock = new WebSocket('ws://sockethost:9999/trips');

    sock.onmessage = function(event) {
      var message = event.data || {};
      if (message.model === 'Trip') {
        switch (message.verb) {
          case 'create':
            console.log('CREATE -> add to collection');
            break;

          case 'destroy':
            console.log('DELETE -> remove from collection');
            break;

          case 'update':
            console.log('UPDATE -> update item in collection');
            break;
        }
      }
    };
  });
});

Any suggestions or ideas around this?

facultymatt avatar Oct 22 '14 20:10 facultymatt

Just an update on this - I've created a pretty mean socket mixin... I will post the code soon.

It mixes into any model and exposes a $subscribe method.

facultymatt avatar Nov 03 '14 18:11 facultymatt

nice!, looking forward to see it!

blackjid avatar Nov 03 '14 19:11 blackjid

@facultymatt - did you ever post this mixin anywhere? I'm keen to check it out!

thekiwi avatar Jan 18 '16 09:01 thekiwi