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