Download the PHP package obray/socket-server without Composer
On this page you can find all versions of the php package obray/socket-server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download obray/socket-server
More information about obray/socket-server
Files in obray/socket-server
Package socket-server
Short Description Generic socket server with non-blocking reads and writes.
License MIT
Informations about the package socket-server
obray/socket-server
Small PHP socket server library with non-blocking reads and queued writes.
This package is intentionally low level. It accepts socket connections, invokes a handler contract, and exposes a small timer and logging surface for libraries or applications built on top of it. Version 1.x is intentionally single-process and event-loop based.
Install
Echo Server
You can also pass the handler directly:
Handler Contract
Handlers implement obray\interfaces\SocketServerHandlerInterface.
The base handler is quiet by default. Pass a logger if you want lifecycle output:
Key callbacks:
onStart($server)runs after the server is bound and the event loop exists.onData($data, $readLength, $connection)runs when data is read.onConnect,onConnected, andonConnectFailedcover connection setup.onDisconnectandonDisconnectedcover connection cleanup.onReadFailedandonWriteFailedexpose IO failures.
Queued Writes
Use qWrite($data) to enqueue bytes for non-blocking writes.
Use qDisconnect() when a connection should close after queued writes flush.
Use disconnect() when the connection should close immediately.
Timers
Register timers from onStart():
Timers are event-loop callbacks. Keep them quick and push long-running work out to your application.
Logging
Server status output can be disabled:
Or routed through a logger:
Event Loop
The server uses the built-in stream_select event loop by default. If the EV
extension is installed, you can request it explicitly:
Smoke Test
Run the local echo smoke test:
Production Notes
- Run long-lived servers under a process supervisor.
- Keep protocol-specific concerns in higher-level libraries or handlers.
- Keep application state in your handler or application container.
- Keep timer callbacks short.
- Use separate worker processes at the application/process-manager layer if you need horizontal concurrency.