In my implementation below I try get the token from ember-simple-auth-token
and use with a URL, used to connect with ActionCable:
...
export default Route.extend({
store: service(),
currentUser: service(),
session: service(),
cable: service(),
setupConsumer: on('init', function() {
let token = this.get('session.data.authenticated.jwt');
let consumer = this.get('cable')
.createConsumer(`wss://api.${config.APP.host}/cable?token=${token}`);
let channelMixin = Mixin.create({
received(data) {
this.get('store').pushPayload(data);
}
});
consumer.subscriptions.create({
channel: 'ChatroomsChannel'
}, channelMixin);
}),
...
});
This works only at first request. I need store this in a cookie? Thanks.
The follow code solves my issue:
import Route from 'ember-route';
import service from 'ember-service/inject';
import Mixin from 'ember-metal/mixin';
import config from 'apollo-enterprise/config/environment';
export default Route.extend({
session: service(),
cable: service(),
afterModel(model) {
this.get('session.store').restore().then((data) => {
let token = data.authenticated.jwt;
let consumer = this.get('cable')
.createConsumer(`wss://api.${config.APP.host}/cable?token=${token}`);
let channelMixin = Mixin.create({
store: service(),
received(data) {
this.get('store').pushPayload(data);
}
});
consumer.subscriptions.create({
channel: 'MessagesChannel',
chatroom_id: model.id
}, channelMixin);
});
},
setupController(controller) {
this._super(...arguments);
controller.set('message', {});
},
actions: {
sendMessage(params) {
let chatroom = this.controller.get('model');
let message = this.get('store').createRecord('message', params);
message.set('chatroom', chatroom);
message.save().then(() => {
this.controller.set('message', {});
});
}
}
});