Question

Is there any tutorial on how to use Sock.js with Ember.js? I can't find any. Any example, tutorial or guide would be nice. I can use sock.js in plain js like:

var sock = new SockJS('/echo');
sock.onmessage = function(e){
    console.log(e.data);
}
sendButton.onclick = function() {
    var text = field.value;
    sock.send(text);
}

But don't know how to use it with Ember.js

Was it helpful?

Solution

In Ember you'd probably want to have a high level controller/route that sets up the sock. Then when a message comes in, assuming that the data property of the message is the JSON representation of a model you can use push on the store. Something like this should do it:

App.Message = DS.Model.extend({...});

App.ApplicationRoute = Ember.Route.extend({
  setupController : function(controller, model){
    var sock = new SockJS('/echo');
    var store = this.get('store');
    sock.onmessage = function(e){
      store.push('message',e.data);
    }
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top