Build Real-Time TCP and WebSocket Services with GatewayWorker on Windows
This guide introduces GatewayWorker, a PHP‑based distributed TCP long-connection framework, explains its Gateway-Worker process model, and provides step-by-step Windows demo instructions for both TCP and WebSocket protocols, highlighting deployment, code changes, testing procedures, and suitable real-time communication scenarios.
Overview
GatewayWorker is a distributed TCP long‑connection framework built on Workerman, ideal for fast development of push services, instant messaging, game servers, IoT, smart home, and similar applications.
It uses a classic Gateway‑Worker process model: the Gateway process maintains client connections and forwards client data to BusinessWorker processes, which contain the business logic (by default handled in Events.php) and push results back to the appropriate client. Gateway and BusinessWorker can be deployed on separate machines to form a distributed cluster.
The API allows global broadcast, group broadcast, or targeted client push, and can be combined with Workerman timers for scheduled pushes.
Official Demo (Windows)
TCP protocol
Download the demo: https://www.workerman.net/download/GatewayWorker.zip Unzip GatewayWorker.zip Enter the GatewayWorker directory.
Run start_for_win.bat to start the server.
Open several command‑line windows and run telnet 127.0.0.1 8282; typing any text will be echoed between clients, confirming the TCP connection works.
WebSocket protocol
Edit start_gateway.php to use the WebSocket protocol:
$gateway = new Gateway("websocket://0.0.0.0:8282");Restart with start_for_win.bat.
Test from a browser console:
var ws = new WebSocket('ws://127.0.0.1:8282');
ws.onmessage = function(event) {
console.log('Received message: ' + event.data);
};Working Principle
Register, Gateway, and BusinessWorker processes start.
Gateway and BusinessWorker register themselves with the Register service via long connections.
Register stores all Gateway addresses in memory.
When BusinessWorker registers, Register sends the list of Gateway addresses to it.
BusinessWorker connects to each Gateway.
New Gateways added later are broadcast to all BusinessWorkers, which then connect.
If a Gateway goes offline, Register removes its address and notifies all BusinessWorkers.
Thus Gateway and BusinessWorker maintain persistent connections through Register.
All client events and data are forwarded by Gateway to BusinessWorker, which processes them in Events.php (onConnect, onMessage, onClose, etc.).
BusinessWorker’s business‑logic entry points are defined in Events.php, covering process lifecycle and client events.
Process Model
Features
The diagram shows Gateway receives client connections and data, forwards data to Workers for processing, and then sends results back through Gateway to other clients. Multiple routing paths enable communication between any two clients.
Advantages
Easy implementation of client‑to‑client communication.
Gateway and Workers communicate via long‑living socket connections, allowing deployment on different servers and effortless horizontal scaling.
Gateway handles only network I/O; business code runs in Workers, which can be reloaded without affecting users, enabling hot code updates.
Applicable Scenarios
Suitable for projects that require real‑time communication between clients.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
