Connect to actioncable with external application

29 views Asked by At

I'd like to allow external applications to connect to my ruby on rails websocket (action_cable) via 'wss://my-domain.com/chat'.When i try it inside ruby environment i can send and receive websocket messages and everything works as expected.

But when i try to connect to websocket with an external application (like some browser extensions for testing websockets) it wont connect but i can see the messages these tools are sending in the consolo logging.

I guess it's because of any ruby on rails security stuff so i added

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*' 
    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

I'm starting the rails server (in development) including the -b '0.0.0.0' option.

The chat.coffee looks like this:

App.chat = App.cable.subscriptions.create "ChatChannel",
  connected: ->
    # Called when the subscription is ready for use on the server
    # callbackWS('true');
    console.log('connected');

  disconnected: ->
    # Called when the subscription has been terminated by the server
    console.log('disconnected');

  received: (data) ->
    addStuff(data['message']);
    # $('.messages').append('<div>' + data['message'] + '</div>');
    # Called when there's incoming data on the websocket for this channel

chat_channel.rb

class ChatChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_from "chat"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end
0

There are 0 answers