Building an Asynchronous UDP SIP Server with Workerman in PHP
This guide explains how to use the high‑performance Workerman framework to create a UDP‑based asynchronous SIP server in PHP, covering protocol basics, business requirements, socket handling, multi‑process deployment, heartbeat management, and sample output.
Overview
The article demonstrates how to implement an asynchronous SIP server over UDP using Workerman, a high‑performance PHP socket framework suitable for long‑connection and high‑concurrency network applications.
Understanding SIP
SIP (Session Initiation Protocol) is a signaling protocol that initiates, manages, and terminates real‑time sessions such as voice, video, and messaging over IP networks. It follows a request/response model similar to HTTP.
Business Requirements
The author needed a UDP‑based SIP server to complement existing Workerman+Channel micro‑services handling TCP/UDP/HTTP for IoT devices. Requirements include persistent UDP communication, support for GB28181 national standard, and integration with media servers like SRS, ZLMediaKit, or monibuca for camera/DVR access and possible FreeSwitch‑based voice/video calls.
Implementation Details
When only one worker process is used, socket functions handle UDP listening; with multiple processes, stream_socket_server is used to allow shared port binding.
Worker count should match CPU cores for optimal performance.
Heartbeat: a timer sends keep‑alive packets at least every 60 seconds to prevent Linux kernel session timeout.
Code Example
<?php
chdir(dirname($_SERVER['SCRIPT_FILENAME']));
include_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
$worker = new Worker();
$worker->count = 1; // number of processes
$processName = "sip_server_udp5060";
$worker->transport = "udp";
$worker->name = $processName;
$worker->reusePort = true;
// ... (configuration, event loop, timers, socket creation, send/receive functions) ...
Worker::runAll();The script sets up global timers to clean up unauthorized or idle sessions, sends periodic test data, and logs received SIP messages. It distinguishes between registered (authenticated) connections stored in $worker->connections and unauthenticated ones in $worker->connections_ur, applying retry limits and timeout policies.
Testing and Sample Output
Initial tests show that with stream_socket the server can reliably send data for about five minutes before the kernel drops the session, while using socket allows longer transmission if firewalls/NAT do not expire the session.
Sample console logs display SIP REGISTER requests with timestamps, source IPs, and SIP headers, confirming that the server correctly receives and processes UDP packets.
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.
