Updating models with websocket service


Currently I’m subscribing to a websocket subscription in setupController which adds a subscription handler that updates the model.

I figured it should be in a service so it can be more modular. I moved the code to a service but then I can’t access the model because of the async data. Previously I waited for the nested relationship data to come through with .then() then started updating the data.

It would be nice if it was like this:

  setupController: function(controller, model) {

(The model contains the relevant data for which websocket channel to subscribe to)

It would be great if I can just inject the service and tell it what model to update via the websocket subscription. There may be multiple models with different subscriptions in a route.

Honestly I don’t know if I’m going about it the right way.


I’m now thinking it shouldn’t be a service because I don’t want to keep the subscription if the user goes to a different page. Where do I keep this code?

I have a component that builds a table from a model that I want to keep updated with the stream. The table is used on multiple pages. It seems wrong to be hard coding this into each route/controller where I want to display it.


We do something kinda similar but with polling instead of sockets (at least until we finish socket support on the API). We keep our subscription code in a service and each view/component that wants to subscribe invokes the service with a method called “managedSubscribe” or something like that with args name (unique name of the view or component or whatever) and what it wants to subscribe to. Then we use the setupController and willTransition hooks to subscribe/unsubscribe in routes, and other methods if subscribing in a component (depending on the use case).

The service simply pushes the data into ember data, and then we use that data wherever we want without trying to “get” it from the service, if that makes any sense. For example in some routes it is attached to our model data via relationships, and it others we just do a “peekAll” with or without filters in the model hook. That way Ember Data is still the interface where you’re fetching the data from, and the service merely defines how the data subscription behaves.


I got this working by passing a model to the service. It wasn’t easy because of having to do this.get everywhere.

I had to use get whenever I wanted to access the model from the argument. Another thing to trip me up on the way…

subscribe: function(model) { let name = model.get(‘name’);