Use your TCP connections with working timeout.


This gem implements a customizable TCP client class that gives you control over time limits. You can set time limits for individual read or write calls or set a deadline for entire call sequences. It has a very small footprint, no dependencies and is easily useable.


require 'tcp-client'

# create a configuration:
# - don't use internal buffering
# - use TLS 1.2 or TLS 1.3
cfg = TCPClient::Configuration.create(
  buffered: false,
  ssl_params: {min_version: :TLS1_2, max_version: :TLS1_3}

# request to
# - limit all network interactions to 1.5 seconds
# - use the Configuration cfg
# - send a simple HTTP get request
# - read the returned message and headers
response =
  TCPClient.with_deadline(1.5, '', cfg) do |client|
    client.write("GET / HTTP/1.1\r\nHost:\r\n\r\n") #=> 40
    client.readline("\r\n\r\n") #=> see response


For more samples see the examples dir


Use Bundler to add TCPClient in your own project:

Include in your Gemfile:

gem 'tcp-client'

and install it by running Bundler:


To install the gem globally use:

gem install tcp-client

After that you need only a single line of code in your project to have it on board:

require 'tcp-client'