lore-hook-websockets-actioncable

WARNING! This hook is a proof of concept, and has never been used in a production application. It was developed to prove that Lore's architecture accounted for the unique concerns that come with using websockets in a real application, and that the interface developed could work with ActionCable.

Provides an implementation of lore-websockets designed to work with the ActionCable implementation introduced in Rails 5

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({

  // serverUrl: 'http://localhost:1337/cable',
  // channel: 'PostsChannel',

  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);
      }
    });
  }
});