Click here to Skip to main content
14,608,056 members

Creating a simple chat using CppComet

Rate this:
4.60 (5 votes)
Please Sign up or sign in to vote.
4.60 (5 votes)
13 Apr 2017MIT
Using CppComet to create a simple chat. CppComet is open source comet server. It is written in C++


Comet technology – allows sending arbitrary messages to client through server initiative.

CppComet is open source comet server with AGPL license. It is written in C++ and uses mysql to store data. With using CppComet we can deliver message from server (for example from php code) by websockets and resive them in JavaScript.

It's easier than it might seem from the beginning. In this article, we will create a simple chat on php and CppComet


Build from source

Recommended Installing to ubuntu, debian or centos. We will be used ubuntu

apt-get update
apt-get install cmake make cpp gcc libssl-dev g++ nginx libmysqlclient-dev mysql-server mysql-client flex mailutils uuid-dev
git clone
cd comet-server
cmake .

Creating databse

CppComet use mysql database for storage users credentials for authorization on server. And to store the time when the user was on the online. And for storing temporary data, such as undelivered messages and other data.

Create a database in mysql based on db.sql file In comet.conf file, set the details to access the database

Run server

Run in console mode


Running in daemon mode

systemctl start comet.service

Add to Startup

cp ./comet.service /etc/systemd/system
systemctl daemon-reload
systemctl enable comet.service

After successes run server we can begin create chat. If you get error on this step create issue in github repository.

Scheme of chat

Tipycal scheme of chat:

Image 1

  1. Connecting to the comet server by websockets
  2. Send ajax message for add new massage to chat
  3. Add message to database
  4. Send message to CppComet
  5. CppComet send messages for all subscribers in pipe

Step 1. Connecting to the comet server from JavaScript api

CppComet has cloud saas alternative that can be used for testing and demo access. In the following examples I will use demonstration access from for those who could not or were too lazy to deploy the server on their vps

Login: 15

For connecting to the comet server from JavaScript api use this command:

cometApi.start({node:"", dev_id:15})
  • in parametr node - set hostname of yure ouwn server
  • parametr dev_id - use only if yure use saas servece

Step 2.1 send message to server

Send ajax query to php back-end:

function sendMessage(name, text)
        url: "",
        type: "POST", 

Step 2.2 Send CometQL query for comet server

CometQL - it’s an API for work with comet server through MySQL protocol. (more info about CometQL)

Advantages of CometQL:

  • Unified API for more than 12 programming languages.
  • Simple and intelligible query view.
  • PHP includes resources for maintaining persistent connections with MySQL and now you can use it for co-working with comet server.

Conneting to comet server through MySQL protocol:

// Credentials for demo access
$host = "";
$user = "15";
$password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";

// Connecting
$comet = mysqli_connect($host, $user, $password, "CometQL_v1");
    echo "Error:".mysqli_error($link);

Send CometQL query for comet server for send message to other users:

// Receive data from $_POST array
$msg = Array( "name" => $_POST["name"], "text"  => $_POST["text"] );
$msg = json_encode($msg);
$msg = mysqli_real_escape_string($comet, $msg);

// Query string
$query = "INSERT INTO pipes_messages (name, event, message)" .
  "VALUES('simplechat', 'newMessage', '".$msg."')";

// Send query
mysqli_query($comet, $query); 
    echo "Error:".mysqli_error($comet);
    echo "ok";

Step 3. receive message from comet server

Subscription code to the pipe on comet server. This callback will be called when somebody send message into channel simplechat with event name newMessage.

cometApi.subscription("simplechat.newMessage", function(event){

Code for filtration received data:

function HtmlEncode(s)
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;


In this article, I told you about using CppComet to create a simple chat. I hope it was interesting.

The CppComet project has many functions that we have not used in this article. It is:

  • Authorization on comet server
  • Getting time when user was online
  • Tracking users status from javascript api in real time
  • Sending message from javascript api
  • Sending private message for user by his id
  • And other functions


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Trapenok Victor
Russian Federation Russian Federation
Developer PHP, JavaScript, C++

Comments and Discussions

QuestionAvast Antivirus blocks access Pin
eslipak18-Apr-17 8:04
professionaleslipak18-Apr-17 8:04 
AnswerRe: Avast Antivirus blocks access Pin
Trapenok Victor19-Apr-17 5:08
MemberTrapenok Victor19-Apr-17 5:08 
GeneralRe: Avast Antivirus blocks access Pin
eslipak19-Apr-17 8:52
professionaleslipak19-Apr-17 8:52 
GeneralRe: Avast Antivirus blocks access Pin
Trapenok Victor19-Apr-17 19:47
MemberTrapenok Victor19-Apr-17 19:47 
GeneralRe: Avast Antivirus blocks access Pin
eslipak20-Apr-17 8:34
professionaleslipak20-Apr-17 8:34 
PraiseGreat Pin
Helen5213-Apr-17 5:47
MemberHelen5213-Apr-17 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 12 Apr 2017


1 bookmarked