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
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 https://github.com/Levhav/comet-server cd comet-server cmake . make
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 in console mode
./cpp_comet
Running in daemon mode
systemctl start comet.service
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.
Tipycal scheme of chat:

CppComet has cloud saas alternative that can be used for testing and demo access. In the following examples I will use demonstration access from comet-server.com for those who could not or were too lazy to deploy the server on their vps
Login: 15 Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8 Host: app.comet-server.ru
For connecting to the comet server from JavaScript api use this command:
cometApi.start({node:"app.comet-server.ru", dev_id:15})
Send ajax query to php back-end:
function sendMessage(name, text)
{
$.ajax({
url: "http://comet-server.org/doc/CppComet/chat-example/chat.php",
type: "POST",
data:"text="+encodeURIComponent(text)+"&name="+encodeURIComponent(name)
});
}
CometQL - it’s an API for work with comet server through MySQL protocol. (more info about CometQL)
Advantages of CometQL:
Conneting to comet server through MySQL protocol:
// Credentials for demo access
$host = "app.comet-server.ru";
$user = "15";
$password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
// Connecting
$comet = mysqli_connect($host, $user, $password, "CometQL_v1");
if(mysqli_errno($comet))
{
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);
if(mysqli_errno($comet))
{
echo "Error:".mysqli_error($comet);
}
else
{
echo "ok";
}
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){
$("#web_chat").append('<b>'+HtmlEncode(event.data.name)+'</b>')
$("#web_chat").append('<i>'+HtmlEncode(event.data.text)+'</i>')
$("#web_chat").append('<br>')
})
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: