Skip to content


Subversion checkout URL

You can clone with
Download ZIP
PHP WebSocket Chat
Branch: master
Failed to load latest commit information. first commit
chat.js first commit
chatclient.php first commit
chatserver.php first commit
error.png first commit
favicon.ico first commit
style.css first commit
system.png first commit
tip.png first commit


Based on the first implementation of WebSockets in PHP, here comes a Web Chat with a little twist.
It uses little monsters as avatars thanks to gravatar and monster_id.

Get the code from the download zone!

Again, as of Feb/10 the only browsers that support websockets are Google Chrome and Safari nightlies.

Client side

var host = "ws://localhost:12345/phpwebsocketchat/chatserver.php";
  socket = new WebSocket(host);
  socket.onopen    = function(evt){ welcome(); };
  socket.onmessage = function(evt){ process(; };
  socket.onclose   = function(evt){ goodbye(); };
catch(ex){ log(ex); }

View source code of chatclient.php

Server side

list($resource,$host,$origin) = getheaders($buffer);
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
           "Upgrade: WebSocket\r\n" .
           "Connection: Upgrade\r\n" .
           "WebSocket-Origin: " . $origin . "\r\n" .
           "WebSocket-Location: ws://" . $host . $resource . "\r\n" .
$handshake = true;

View source code of chatserver.php

Steps to run the test:

  • Download all files to a folder in your local server running Apache and PHP.
  • From the command line, run the chatserver.php program to listen for socket connections.
  • Open Google Chrome and point to the chatclient.php page
  • Done, your browser now has a full-duplex channel with the server.
  • Start chatting with your friends in real time!

WebSockets for the masses!


George Nava

Something went wrong with that request. Please try again.