Herocu Websocket for custom domain

I am using ActionCable with Rails on Heroku.

Chat works fine when I am on example.herokuapp.com, but it breaks in my own domain (example.com) because I DO NOT issue cookies from a web socket site (wss: //example.heroku.com/ cable )

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

Alternatively, could you explain how to use the cloudflare web memory features to solve this problem?

Thank you very much.

+6
source share
1 answer

When working with websocket, I really like working with pusher . They have dinner easy to set up. they are free for 200 thousand messages per day

This is the heroku documentation for the pusher

1) set the gem

gem install pusher 

2) install 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 the keys, all you need to do is create a client (js) and a server

the code on the client is similar to 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 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 this helps

0
source

Source: https://habr.com/ru/post/1012877/


All Articles