7

I'm using ActionCable with Rails on Heroku.

Chat works perfectly when I am at example.herokuapp.com BUT it breaks on my custom domain (example.com) because I am NOT issuing a cookie from the host of the web socket (wss://example.heroku.com/cable).

Is there any way to point wss://example.herokuapp.com/cable TO wss://example.com/cable on Heroku?

Alternatively, would you explain how to use cloudflare's websocket capabilities to workaround this problem?

Thank you so much.

Austin
  • 71
  • 1

1 Answers1

-1

When doing work with websocket I really like to work with pusher. They are supper simple to set up. they are free for 200k Messages / Day

This is the heroku documentation for pusher

1) install the gem

gem install pusher

2) install the addons

heroku addons:create pusher:sandbox

3) set your initializers

#config/initializers/pusher.rb
require 'pusher'

Pusher.app_id = '000000'
Pusher.key = '000000000000000000'
Pusher.secret = '00000000000000000000'
Pusher.cluster = 'xxx'
Pusher.logger = Rails.logger
Pusher.encrypted = true

Once you have your keys in place all you need is to create a client (js) and a server

the code in the client is like this

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/4.1/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher("#{key}", {
      cluster: "#{cluster}",
      encrypted: true
    });

    var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
      alert(data.message);
    });
  </script>
</head>

now on the server is really simple

require 'pusher'

pusher_client = Pusher::Client.new(
  app_id: ENV[:app_id],
  key: ENV[:key],
  secret: ENV[:secret],
  cluster: ENV[:cluster],
  encrypted: true
)

pusher_client.trigger('my-channel', 'my-event', {
  message: 'hello world'
})

I hope that this helps out

MZaragoza
  • 10,108
  • 9
  • 71
  • 116