lore-hook-websockets-actioncable
Source code for this hook can be found on GitHub at this link.
Purpose
This hook is built on top of the lore-websockets library and implements the interface for ActionCable.
Example
See the websockets example to see this hook in action.
Example Usage
The ActionCable hook behaves identical to the SocketIO hook, except for the implementation. The big callout here is the interface that ActionCable uses, which is a bit different than the other two implementations.
Instead of using namespaces
, ActionCable uses channels
. I think they're equivalent, but not sure. Meaning Socket.io breaks data up into namespaces & rooms, but ActionCable seems to only use one of those (?). I played with it only enough to get this to work and see if the interface could adapt to something not based in Node : )
import _ from 'lodash';
import { WebSocketConnection } from 'lore-websockets';
import ActionCable from 'actioncable';
export default WebSocketConnection.extend({
connect: function() {
this.cable = ActionCable.createConsumer(this.serverUrl);
},
subscribe: function subscribe() {
var that = this;
this.cable.subscriptions.create(this.channel, {
connected: function() {
console.log('ActionCable:WebSocket - connected!')
},
disconnected: function() {
console.log('ActionCable:WebSocket - disconnected!')
},
received: function(data) {
that.dispatch(data);
}
});
}
});