Project

unix_socks

0.0
No release in over 3 years
This library enables communication between processes using Unix sockets. It handles message transmission, socket management, and cleanup, supporting both synchronous and asynchronous operations while providing error handling for robust development.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
~> 1.20
~> 3.2
>= 0

Runtime

~> 2.0
~> 1.3
 Project Readme

UnixSocks 🧦🧦

Description

A Ruby library for handling Unix socket-based communication.

Features

  • Message Handling: Simplify sending and receiving messages over Unix sockets.
  • Dynamic Method Access: Access message body values using method names (e.g., message.key).
  • Background Processing: Run the server in a background thread to avoid blocking main execution.
  • Robust Error Handling: Gracefully handle socket disconnections and JSON parsing errors.

Installation

Add this gem to your Gemfile:

gem 'unix_socks'

And install it using Bundler:

bundle install

Or install the gem directly:

gem install unix_socks

Usage

1. Server Setup

Create a server instance and start listening for connections:

require 'unix_socks'

server = UnixSocks::Server.new(socket_name: 'my_socket')

# Run the server in the background to avoid blocking
thread = server.receive_in_background(force: true) do |message|
  puts "Received message: #{message.inspect}"
end

thread.join

2. Sending Messages

Transmit messages to connected clients:

client = UnixSocks::Server.new(socket_name: 'my_socket')

# Prepare your message
message = { status: 'success', data: [1, 2, 3] }

# Send the message
client.transmit(message)

3. Responding to Messages

Handle incoming messages and send responses:

require 'unix_socks'

server = UnixSocks::Server.new(socket_name: 'my_socket')

def handle_message(message)
  # Access message body values using method names
  puts "Received status: #{message.status}"
  
  # Send a response
  message.respond({ status: 'acknowledged' })
end

# Use in your server setup
thread = server.receive_in_background(force: true) do |message|
  handle_message(message)
end

thread.join

And in the client:

client = UnixSocks::Server.new(socket_name: 'my_socket')

# Prepare your message
message = { status: 'success', data: [1, 2, 3] }

# Send the message
response = client.transmit_with_response(message)

# Receive the response
puts "Received server response status: #{response.status}"

4. Message Object Features

  • Dynamic Access: Methods like message.status automatically map to the message body.
  • Disconnect Handling: Safely close socket connections using disconnect.
  • Error Resilience: The respond method handles disconnections gracefully.

Author

Florian Frank

License

MIT License