Download the PHP package ody/server without Composer
On this page you can find all versions of the php package ody/server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package server
Ody Server
Ody Server is a package for the Ody PHP framework that provides high-performance HTTP server capabilities powered by Swoole. It offers easy server management with command-line tools, hot-reloading support, and administrative features.
Table of Contents
- Introduction
- Installation
- Basic Usage
- Configuration
- Server Management
- Server Events
- Admin Server
- API Reference
Introduction
Ody Server simplifies the process of creating and managing Swoole-based servers. It provides:
- Command-line tools for managing server processes
- HTTP, WebSocket, and TCP server support
- Hot reloading capabilities for development
- Server state management
- Administrative API interface for monitoring and management
Installation
Basic Usage
Starting a Server
To start a server, use the server:start
command:
Options:
--daemonize
or-d
: Run the server in the background--watch
or-w
: Enable file watching for hot reloading
Stopping a Server
To stop a running server:
Reloading a Server
To reload workers without stopping the server:
Configuration
Server configuration should be defined in your application's config/server.php
file:
Server Management
The ServerManager
class provides an interface for creating and managing Swoole servers.
Server Events
The following server events can be registered in your configuration:
Event | Description |
---|---|
ON_START |
Triggered when the server starts |
ON_WORKER_START |
Triggered when a worker process starts |
ON_WORKER_STOP |
Triggered when a worker process stops |
ON_WORKER_EXIT |
Triggered when a worker process exits |
ON_WORKER_ERROR |
Triggered when a worker process encounters an error |
ON_PIPE_MESSAGE |
Triggered when a message is sent through pipes |
ON_REQUEST |
Triggered when an HTTP request is received |
ON_RECEIVE |
Triggered when data is received |
ON_CONNECT |
Triggered when a client connects |
ON_DISCONNECT |
Triggered when a client disconnects |
ON_OPEN |
Triggered when a WebSocket connection is opened |
ON_MESSAGE |
Triggered when a WebSocket message is received |
ON_CLOSE |
Triggered when a connection is closed |
ON_TASK |
Triggered when a task is received |
ON_FINISH |
Triggered when a task is finished |
ON_SHUTDOWN |
Triggered when the server shuts down |
ON_PACKET |
Triggered when a UDP packet is received |
ON_MANAGER_START |
Triggered when the manager process starts |
ON_MANAGER_STOP |
Triggered when the manager process stops |
ON_BEFORE_START |
Triggered before the server starts (not a Swoole event) |
Server State
The HttpServerState
class manages the state of running server processes, allowing for tracking and management of
processes.
Admin Server
!! Use with caution, very experimental and likely to break !!
The AdminServer
class provides an administrative interface for monitoring and managing Swoole servers. It exposes a
RESTful API for interacting with the server processes.
To enable the Admin server, add the following to your server configuration:
You can access a web-based dashboard at http://your-admin-server:port/dashboard
.
Authentication
The admin server supports authentication via the admin_server
URI:
When authenticated, an access token is generated as sha1(username . password)
and must be provided in the
X-ADMIN-SERVER-ACCESS-TOKEN
HTTP header.
API Endpoints
The admin server exposes a RESTful API at the /api
endpoint. The URL structure is:
Where:
COMMAND
is one of the registered command namesPROCESS
identifies the target process (e.g., "master", "worker-0", "all")
Available Commands
Command | Description | HTTP Method |
---|---|---|
server_reload |
Reload worker processes | POST |
server_shutdown |
Shut down the server | POST |
server_stats |
Get server statistics | GET |
server_setting |
Get server configuration | GET |
coroutine_stats |
Get coroutine statistics | GET |
coroutine_list |
Get active coroutines | GET |
coroutine_bt |
Get coroutine backtrace | POST |
get_version_info |
Get version information | GET |
get_worker_info |
Get worker process information | GET |
get_timer_list |
Get active timers | GET |
get_server_memory_usage |
Get memory usage | GET |
get_server_cpu_usage |
Get CPU usage | GET |
close_session |
Close a client connection | POST |
get_client_info |
Get client information | GET |
Example Requests
Retrieve server statistics:
Reload the server:
Get coroutine list from all worker processes:
Close a client connection:
API Reference
Server Types
The ServerType
class provides constants for different server types:
HTTP_SERVER
: Swoole HTTP server (\Swoole\Http\Server
)WS_SERVER
: Swoole WebSocket server (\Swoole\WebSocket\Server
)TCP_SERVER
: Swoole TCP server (\Swoole\Server
)
Server Events
The ServerEvent
class provides constants for all supported server events:
ON_START
: Server start eventON_WORKER_START
: Worker process start eventON_WORKER_STOP
: Worker process stop eventON_WORKER_EXIT
: Worker process exit eventON_WORKER_ERROR
: Worker process error eventON_PIPE_MESSAGE
: Pipe message eventON_REQUEST
: HTTP request eventON_RECEIVE
: Data receive eventON_CONNECT
: Client connect eventON_DISCONNECT
: Client disconnect eventON_OPEN
: WebSocket open eventON_MESSAGE
: WebSocket message eventON_CLOSE
: Connection close eventON_TASK
: Task eventON_FINISH
: Task finish eventON_SHUTDOWN
: Server shutdown eventON_PACKET
: UDP packet eventON_MANAGER_START
: Manager start eventON_MANAGER_STOP
: Manager stop eventON_BEFORE_START
: Before server start event (not a Swoole event)
ServerManager Class
Methods:
init(string $serverType): static
- Initialize the server manager with a server typecreateServer(?array $config): static
- Create a new server instance with the given configurationsetServerConfig(array $config): static
- Set additional server configurationgetServerInstance(): HttpServer|WsServer
- Get the server instanceregisterCallbacks(array $callbacks): static
- Register event callbackssetWatcher(int $enableWatcher, array $paths, object $serverState): static
- Enable file watching for hot reloadingdaemonize(bool $daemonize): static
- Set the server to run in the backgroundsetLogger(LoggerInterface $logger): self
- Set the logger instancesetConfig(Config $config): self
- Set the configuration instancestart(): void
- Start the server
ServerState Class
Methods:
getInstance(): self
- Get the singleton instancegetInformation(): array
- Get the state informationsetManagerProcessId(?int $id): void
- Set the manager process IDsetMasterProcessId(?int $id): void
- Set the master process IDsetWatcherProcessId(?int $id): void
- Set the watcher process IDsetWorkerProcessIds(array $ids): void
- Set the worker process IDsgetManagerProcessId(): int|null
- Get the manager process IDgetMasterProcessId(): int|null
- Get the master process IDgetWatcherProcessId(): int|null
- Get the watcher process IDgetWorkerProcessIds(): array
- Get the worker process IDsclearProcessIds(): void
- Clear all process IDsreloadProcesses(array $processIds): void
- Reload the specified processeskillProcesses(array $processIds): void
- Kill the specified processes
HttpServerState Class
Methods:
getInstance(): self
- Get the singleton instancehttpServerIsRunning(): bool
- Check if the HTTP server is running
AdminServer Class
Methods:
init(Server $server): void
- Initialize the admin servergetAccessToken(): string
- Get the access tokenstart(Server $server): void
- Start the admin server
ServerCallbacks Class
Static methods:
onStart(SwServer $server): void
- Handle server start eventonRequest(SwRequest $request, SwResponse $response): void
- Handle HTTP request eventonWorkerStart(SwServer $server, int $workerId): void
- Handle worker start eventonManagerStart(SwServer $server)
- Handle manager start eventonManagerStop(SwServer $server)
- Handle manager stop eventonReceive(SwServer $server, int $fd, int $reactorId, string $data)
- Handle data receive eventonWorkerError(SwServer $server, int $workerId, int $workerPid, int $exitCode, int $signal): void
- Handle worker error event
All versions of server with dependencies
ody/swoole Version ^0.1.0
psr/http-message Version ^2.0
psr/http-server-handler Version ^1.0.2