One of the simplest way to create websockets chat with php

I want to tell you about my Open Source project of comet server. It simplifies the process of creating chat and notifications for the site. The CppComet takes care of all the work of maintaining websocket connections and give simple api for sending messages from backend to frontend by websockets. CppComet is written in C++ and can handle many simultaneous connections. Here are the results of load testing in 64,000 online connections that I conducted.


  • Api for more then 9 languages. CometQL API allows you connect to comet server by mysql protocol.
  • Supports users authenticating on the server
  • Allow sending a private message to users
  • Allows you to send public messages to users via channels
  • Allows subscription from JavaScript to changing other user’s status
  • Allows subscription to updates of online users list in real-time
  • Sending messages to channel from JS
  • Allows get information when users were online
For testing CppComet without install on vps you can use free cloud service with same api. In the all examples I will use demonstration access from for those who could not or were too lazy to deploy the server on their vps. Demo access credentials:
  1. Login: 15
  2. Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8
  3. Host:

How it works

Comet technology – allows sending arbitrary messages to client through server initiative. Browser opens page of your site. And after loading this page JavaScript Api establishes a persistent connection to CppComet using websockets. While page is open, your server can send text message to client. It appeals via CometQL API to CppComet and transfer message for browser. chat sheme
  1. Connecting to the comet server by websockets
  2. Send ajax message for add new massage to chat
  3. Send message to CppComet
  4. CppComet send messages for all subscribers in pipe (and JavaScript API delivers this message to your callback.)
  5. Add message to database (optional)

Chat example

result chat on gif

Step 1. Connecting to the comet server

Code in file index.html To connect to the comet server from the JavaScript API, use the following command: cometApi.start({node:"", dev_id:15})
  • in parameter node - set hostname of your own server
  • parameter dev_id - use only if you use saas service

Step 2. Send ajax message for add new massage to chat

  1. var name = "my name";
  2. var text = "my text message";
  3. $.ajax({
  4. url: "",
  5. type: "POST",
  6. data:"text="+encodeURIComponent(text)+"&name="+encodeURIComponent(name)
  7. });

Step 3. send message to CppComet from php

Code in file chat.php The code for sending a message to the pipe "simplechat" and event 'newMessage':
  1. // Demo access credentials
  2. $host = "";
  3. $user = "15";
  4. $password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
  6. // Connecting to CppComet (this is not MySQL database)
  7. $comet = mysqli_connect($host, $user, $password, "CometQL_v1");
  8. if(mysqli_errno($comet))
  9. {
  10. echo "Error:".mysqli_error($link);
  11. }
  12. $msg = Array( "name" => $_POST["name"], "text" => $_POST["text"] );
  13. $msg = json_encode($msg);
  14. $msg = mysqli_real_escape_string($comet, $msg);
  15. $query = "INSERT INTO pipes_messages (name, event, message)" .
  16. "VALUES('simplechat', 'newMessage', '".$msg."')";
  18. // Sending message to CppComet (this is not MySQL database)
  19. mysqli_query($comet, $query);
  20. if(mysqli_errno($comet))
  21. {
  22. echo "Error:".mysqli_error($comet);
  23. }
  24. else
  25. {
  26. echo "ok";
  27. }

Step 4. Receive message from CppComet in JavaScript

Code in file index.html Subscription Code to the pipe on comet server. This callback will be called when somebody send message into channel `simplechat`
  1. cometApi.subscription("simplechat.newMessage", function(event){
  2. alert(": "
  3. })




With pleasure I will answer questions about the project.

Add new comment