Search code examples
ruby-on-railsrubywebsocketbitcoincoinbase-api

How to send a keep-alive packet through websocket in ruby on rails


I want to send a

"Keep alive from client"

message every 30 seconds for my websocket connection. Here's what the code that I have in my websocket initializer looks like:

ws = WebSocket::Client::Simple.connect 'wss://bitcoin.toshi.io/'

ws.on :message do |msg|
  rawJson = msg.data
  message_response = JSON.parse(rawJson)
end

ws.on :open do
  ws.send "{\"subscribe\":\"blocks\"}"
end

ws.on :close do |e|
  puts "WEBSOCKET HAS CLOSED #{e}"
  exit 1
end

ws.on :error do |e|
  puts "WEBSOCKET ERROR #{e}"
end

Without any sort of 'keep alive', the connect closes in about 45 seconds. How should I send the 'heart-beat' packet? It seems that the connection is closed by their server, not mine.


Solution

  • You can use Websocket Eventmachine Client gem to send hearbeat:

    require 'websocket-eventmachine-client'
    
    EM.run do
      ws = WebSocket::EventMachine::Client.connect(:uri => 'wss://bitcoin.toshi.io/')
      puts ws.comm_inactivity_timeout
      ws.onopen do
        puts "Connected"
      end
    
      ws.onmessage do |msg, type|
        puts "Received message: #{msg}"
      end
    
      ws.onclose do |code, reason|
        puts "Disconnected with status code: #{code}"
      end
    
      EventMachine.add_periodic_timer(15) do
        ws.send "{}"
      end
    end
    

    You can setup timer for EventMachine with EM::add_periodic_timer(interval_in_seconds), and then send your heartbeat with it.