<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.wikijournal.org/w-wiki/index.php?action=history&amp;feed=atom&amp;title=Building_WebSockets_in_PHP_from_Scratch</id>
	<title>Building WebSockets in PHP from Scratch - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://en.wikijournal.org/w-wiki/index.php?action=history&amp;feed=atom&amp;title=Building_WebSockets_in_PHP_from_Scratch"/>
	<link rel="alternate" type="text/html" href="https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;action=history"/>
	<updated>2026-04-30T06:33:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2059&amp;oldid=prev</id>
		<title>Philip at 09:50, 12 March 2025</title>
		<link rel="alternate" type="text/html" href="https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2059&amp;oldid=prev"/>
		<updated>2025-03-12T09:50:33Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:50, 12 March 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l7&quot;&gt;Line 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;At the end of this article, you will find the complete code and a link to a demo chat.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;At the end of this article, you will find the complete code and a link to a demo chat.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/del&gt;== Goals ==&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Goals ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Understand server-side sockets in PHP.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Understand server-side sockets in PHP.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Learn the WebSocket protocol.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Learn the WebSocket protocol.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikijournal_org-en__:diff:1.41:old-2058:rev-2059:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Philip</name></author>
	</entry>
	<entry>
		<id>https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2058&amp;oldid=prev</id>
		<title>Philip at 09:49, 12 March 2025</title>
		<link rel="alternate" type="text/html" href="https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2058&amp;oldid=prev"/>
		<updated>2025-03-12T09:49:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;amp;diff=2058&amp;amp;oldid=2057&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Philip</name></author>
	</entry>
	<entry>
		<id>https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2057&amp;oldid=prev</id>
		<title>Philip: Created page with &quot;Some time ago, I was looking for a library to work with WebSockets in PHP. During my research, I came across multiple articles discussing Node.js integration with Yii, while most WebSocket-related articles limited themselves to instructions on using phpdaemon.  I explored libraries like phpdaemon and Ratchet. They seemed overly complex, especially since Ratchet recommends using WAMP for sending messages to specific users. I couldn&#039;t understand why such heavyweight soluti...&quot;</title>
		<link rel="alternate" type="text/html" href="https://en.wikijournal.org/w-wiki/index.php?title=Building_WebSockets_in_PHP_from_Scratch&amp;diff=2057&amp;oldid=prev"/>
		<updated>2025-03-12T09:45:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Some time ago, I was looking for a library to work with WebSockets in PHP. During my research, I came across multiple articles discussing Node.js integration with Yii, while most WebSocket-related articles limited themselves to instructions on using phpdaemon.  I explored libraries like phpdaemon and Ratchet. They seemed overly complex, especially since Ratchet recommends using WAMP for sending messages to specific users. I couldn&amp;#039;t understand why such heavyweight soluti...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Some time ago, I was looking for a library to work with WebSockets in PHP. During my research, I came across multiple articles discussing Node.js integration with Yii, while most WebSocket-related articles limited themselves to instructions on using phpdaemon.&lt;br /&gt;
&lt;br /&gt;
I explored libraries like phpdaemon and Ratchet. They seemed overly complex, especially since Ratchet recommends using WAMP for sending messages to specific users. I couldn&amp;#039;t understand why such heavyweight solutions were necessary, particularly when they required installing additional dependencies. After reviewing the source code of these and other libraries, I figured out how everything works and decided to write a simple WebSocket server in PHP myself. This helped me reinforce my understanding and discover some hidden pitfalls I hadn&amp;#039;t considered before.&lt;br /&gt;
&lt;br /&gt;
Thus, I set out to build the required functionality from scratch.&lt;br /&gt;
&lt;br /&gt;
At the end of this article, you will find the complete code and a link to a demo chat.&lt;br /&gt;
&lt;br /&gt;
=== Goals ===&lt;br /&gt;
&lt;br /&gt;
# Understand server-side sockets in PHP.&lt;br /&gt;
# Learn the WebSocket protocol.&lt;br /&gt;
# Write a simple WebSocket server from scratch.&lt;br /&gt;
&lt;br /&gt;
== 1) Server-side Sockets in PHP ==&lt;br /&gt;
Before this, I had only a vague understanding of server-side sockets. After reviewing several WebSocket library implementations, I encountered two common approaches:&lt;br /&gt;
&lt;br /&gt;
Using the PHP &amp;lt;code&amp;gt;socket&amp;lt;/code&amp;gt; extension:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // Create socket&lt;br /&gt;
 socket_bind($socket, &amp;#039;127.0.0.1&amp;#039;, 8000); // Bind to IP and port&lt;br /&gt;
 socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); // Allow multiple connections on the same port&lt;br /&gt;
 socket_listen($socket); // Start listening&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Using the PHP &amp;lt;code&amp;gt;stream&amp;lt;/code&amp;gt; extension:&lt;br /&gt;
 &amp;lt;code&amp;gt;$socket = stream_socket_server(&amp;quot;tcp://127.0.0.1:8000&amp;quot;, $errno, $errstr);&amp;lt;/code&amp;gt;&lt;br /&gt;
I preferred the second option for its simplicity.&lt;br /&gt;
&lt;br /&gt;
Now that we have created a server socket, we need to handle incoming connections. There are two main approaches:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Basic while-loop:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
while ($connect = stream_socket_accept($socket, -1)) { // Wait for new connection (no timeout)&lt;br /&gt;
     ... // Handle $connect&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Using &amp;lt;code&amp;gt;stream_select&amp;lt;/code&amp;gt; for multiple connections:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$connects = array();&lt;br /&gt;
 while (true) {&lt;br /&gt;
     $read = $connects;&lt;br /&gt;
     $read[] = $socket;&lt;br /&gt;
     $write = $except = null;&lt;br /&gt;
     &lt;br /&gt;
     if (!stream_select($read, $write, $except, null)) { // Wait for readable sockets (no timeout)&lt;br /&gt;
         break;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     if (in_array($socket, $read)) { // New connection detected&lt;br /&gt;
         $connect = stream_socket_accept($socket, -1);&lt;br /&gt;
         $connects[] = $connect;&lt;br /&gt;
         unset($read[array_search($socket, $read)]);&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     foreach ($read as $connect) { // Process all active connections&lt;br /&gt;
         ... // Handle $connect&lt;br /&gt;
         unset($connects[array_search($connect, $connects)]);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Since we need to handle both new connections and existing ones for incoming messages, we will use the &amp;lt;code&amp;gt;stream_select&amp;lt;/code&amp;gt; approach.&lt;br /&gt;
&lt;br /&gt;
== 2) WebSocket Protocol ==&lt;br /&gt;
A great explanation of the WebSocket protocol can be found in this article. Here, we focus on two key aspects:&lt;br /&gt;
&lt;br /&gt;
=== WebSocket Handshake ===&lt;br /&gt;
To establish a WebSocket connection, we need to read the &amp;lt;code&amp;gt;Sec-WebSocket-Key&amp;lt;/code&amp;gt; header from the client request, compute the &amp;lt;code&amp;gt;Sec-WebSocket-Accept&amp;lt;/code&amp;gt; value, and send a proper response:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
 $SecWebSocketAccept = base64_encode(pack(&amp;#039;H*&amp;#039;, sha1($SecWebSocketKey . &amp;#039;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&amp;#039;)));&lt;br /&gt;
 $response = &amp;quot;HTTP/1.1 101 Web Socket Protocol Handshake\r\n&amp;quot; .&lt;br /&gt;
     &amp;quot;Upgrade: websocket\r\n&amp;quot; .&lt;br /&gt;
     &amp;quot;Connection: Upgrade\r\n&amp;quot; .&lt;br /&gt;
     &amp;quot;Sec-WebSocket-Accept: $SecWebSocketAccept\r\n\r\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Message Encoding and Decoding ===&lt;br /&gt;
When receiving data from a WebSocket, we need to decode it, and when sending data, we must encode it. The encoding process is well-documented in WebSocket specifications, but in practice, we only need two functions: &amp;lt;code&amp;gt;decode()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;encode()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3) Simple WebSocket Server ==&lt;br /&gt;
Now that we have all the necessary components, we can combine the HTTP server logic with handshake, decoding, and encoding functions to create a basic WebSocket server.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Example of a simple WebSocket server:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 &amp;lt;code&amp;gt;// Implementation of a basic WebSocket server&amp;lt;/code&amp;gt;&lt;br /&gt;
This example allows customization of event handlers like &amp;lt;code&amp;gt;onOpen&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;onClose&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;onMessage&amp;lt;/code&amp;gt; for custom functionality.&lt;br /&gt;
&lt;br /&gt;
=== Goals Achieved ===&lt;br /&gt;
With this implementation, we have successfully:&lt;br /&gt;
&lt;br /&gt;
* Understood PHP server sockets.&lt;br /&gt;
* Implemented the WebSocket protocol.&lt;br /&gt;
* Built a simple WebSocket server from scratch.&lt;br /&gt;
&lt;br /&gt;
If you found this material useful, in the next article, I will describe how to run multiple processes for handling connections (one master and several workers), inter-process communication, and integration with frameworks like Yii.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Demo Chat with the Above Functionality&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[Demo Chat Code]&lt;br /&gt;
&lt;br /&gt;
=== Update (Best Comments from Readers): ===&lt;br /&gt;
&lt;br /&gt;
* Each connection consumes about 9KB of memory.&lt;br /&gt;
* Using &amp;lt;code&amp;gt;fgets()&amp;lt;/code&amp;gt; with open sockets can cause &amp;quot;hanging&amp;quot; because WebSocket messages do not end with a newline. Use &amp;lt;code&amp;gt;fread()&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
* When writing a response to a socket using &amp;lt;code&amp;gt;fwrite()&amp;lt;/code&amp;gt;, always check if all bytes were successfully written.&lt;br /&gt;
* Before sending data from the server, check if the client is ready to receive using &amp;lt;code&amp;gt;stream_socket_accept()&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Sending non-UTF-8 characters to the socket will cause the client to disconnect with an error: &amp;lt;code&amp;gt;WebSocket connection to &amp;#039;ws://sharoid.ru:8000/&amp;#039; failed: Could not decode a text frame as UTF-8.&amp;lt;/code&amp;gt;&lt;br /&gt;
* To check if no data was received and the socket should be closed, use &amp;lt;code&amp;gt;!strlen($data)&amp;lt;/code&amp;gt;, not &amp;lt;code&amp;gt;!$data&amp;lt;/code&amp;gt;.&lt;br /&gt;
* You can place an Nginx server in front of the WebSocket server for better performance.&lt;/div&gt;</summary>
		<author><name>Philip</name></author>
	</entry>
</feed>