0

I want to access data from a websocket stream. I can't get ActionCable to bind the message received function. If I hack cable.js then I can see the data by adding:

App.cable = ActionCable.createConsumer("ws://localhost:7000");
App.cable.connect();
App.cable.connection.webSocket.onmessage = function(event) { console.log(event); };

Otherwise I keep getting an error message which is generated every time a message is received:

enter image description here

/app/assets/javascripts/channels/markets.coffee

App.markets = App.cable.subscriptions.create { channel: "MarketsChannel" },
  connected: ->
    console.log("connected");

  disconnected: ->

  received: (data) ->
        console.log("Test");

/app/assets/javascripts/cable.js

//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});
  App.cable = ActionCable.createConsumer("ws://localhost:7000");

  //App.cable.connect();
  //App.cable.connection.webSocket.onmessage = function(event) { console.log(event); };
}).call(this);

Plus this other standard files from the generator.

Why is everything so hard....?

Joshua Jenkins
  • 315
  • 1
  • 15
  • I'm guessing there's a tag that actioncable uses with the event data that says what channel it's for. – Joshua Jenkins Oct 13 '17 at 08:54
  • ActionCable runs it's own protocol as an extra layer over the Websocket connection. This means that you must either 1. use the same protocol on every Websocket client that connects to an ActionCable server, or 2. drop ActionCable in favor of a custom protocol that you will use in every Websocket client you author (which is what I actually prefer and the reason I wrote the iodine Websocket server). – Myst Oct 13 '17 at 19:04
  • Shame that it's not really designed to connect to external websockets. – Joshua Jenkins Oct 13 '17 at 23:58
  • It's possible to connect to external websockets, but you will need to "translate" from the external data stream to the ActionCable layer. – Myst Oct 14 '17 at 00:56
  • I can change the websocket or can I just create an adapter in rails? – Joshua Jenkins Oct 14 '17 at 01:12
  • I don't have enough information to help, since I have no idea what data stream you're connecting to or it's protocol (is it ActiveRecord as well? Does it use different message semantics?)... for example, please ignore all my if `ws://localhost:7000` is an ActionCable server (it must be a different issue I'm not aware of). – Myst Oct 14 '17 at 02:25
  • I just created my own node.js app to connect to the external websocket and rebroadcast on a different port. I can change the format from that end, I think it will be easier. – Joshua Jenkins Oct 14 '17 at 04:40

0 Answers0