Creating a simple chat using CppComet

ImbaChat
CppCpmet
Published in
3 min readAug 19, 2017

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

Background

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 https://github.com/Levhav/comet-server
cd comet-server
cmake .
make

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

./cpp_comet

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:

  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 https://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})
  • in parametr node — set hostname of yure ouwn server
  • parametr dev_id — use only if yure use saas servece comet-server.com

Step 2.1 send message to server

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)
});
}

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 = "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";
}

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){
$("#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;
}

Summary

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

--

--

ImbaChat
CppCpmet
Editor for

We are a web studio specialized in web and software development providing a wide spectrum of services.