The Fluent::PluginHelper::Server module provides a high-level interface for creating plugins that listen on network sockets (TCP, TLS, or UDP). It abstracts away the complexities of event loops and socket management using Cool.io.
Connection-oriented protocols (TCP, TLS)
Use server_create_connection when you need to manage individual client connections. The block provides a connection object that allows you to access remote metadata and handle data streams.
Datagram protocols (UDP)
Use server_create with proto: :udp for connectionless data reception. The callback receives the raw data and a socket object representing the sender.
Supported Protocols
:tcp (Connection-oriented):tls (Secure connection-oriented):udp (Datagram):unix (Unix domain sockets - not yet implemented)
Note: TCP/TLS keepalive and Unix domain sockets are currently not supported.
# Example: TCP Connection-oriented server
server_create_connection(:my_tcp_server, 5170) do |conn|
source_addr = conn.remote_host
source_port = conn.remote_port
conn.data do |data|
# Process incoming data
conn.write "received"
conn.close
end
end
# Example: UDP Datagram server
server_create(:my_udp_server, 5171, proto: :udp, max_bytes: 2048) do |data, sock|
host = sock.remote_host
port = sock.remote_port
# Process data
end