Search code examples
herokutcp

Getting a "Connection closed by foreign host" error when hosting a TCP server on Heroku with Ruppell's Sockets addon


I'm trying to host a simple TCP server on Heroku.

I am using the Ruppell's Sockets Heroku addon. My Procfile looks like this:

web: bundle exec ruby web.rb -e "production" -p $PORT
socket: ./lib/sockets-connect/rs-conn ruby ./tcpserver.rb

tcpserver.rb looks like this:

require 'eventmachine'

 module EchoServer
   def post_init
     puts "-- someone connected to the echo server!"
   end

   def receive_data data
     send_data ">>>you sent: #{data}"
     close_connection if data =~ /quit/i
   end

   def unbind
     puts "-- someone disconnected from the echo server!"
  end
end

EventMachine.run {
  EventMachine.start_server "127.0.0.1", ENV['RUPPELLS_SOCKETS_LOCAL_PORT'], EchoServer
}

RUPPELLS_SOCKETS_FRONTEND_URI is tcp://22405.339f7303-45df-4e9c-a049-5a15c47168ab.sockets.ruppells.io:22405/.

When I do telnet tcp://22405.339f7303-45df-4e9c-a049-5a15c47168ab.sockets.ruppells.io 22405, I get a Connection closed by foreign host..

I have very little experience with TCP. Any idea how I can troubleshoot this?


Solution

  • For anyone else struggling with this, your local server must bind to 0.0.0.0 and not 127.0.0.1.