Comet technology – allows sending arbitrary messages to client through server initiative.
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 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
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
Running in daemon mode
systemctl start comet.service
Add to Startup
cp ./comet.service /etc/systemd/system
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:
- Connecting to the comet server by websockets
- Send ajax message for add new massage to chat
- Add message to database
- Send message to CppComet
- CppComet send messages for all subscribers in pipe
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
- 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)
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:
$host = "app.comet-server.ru";
$user = "15";
$password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
$comet = mysqli_connect($host, $user, $password, "CometQL_v1");
Send CometQL query for comet server for send message to other users:
$msg = Array( "name" => $_POST["name"], "text" => $_POST["text"] );
$msg = json_encode($msg);
$msg = mysqli_real_escape_string($comet, $msg);
$query = "INSERT INTO pipes_messages (name, event, message)" .
"VALUES('simplechat', 'newMessage', '".$msg."')";
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.
Code for filtration received data:
var el = document.createElement("div");
el.innerText = el.textContent = s;
s = el.innerHTML;
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
- Sending private message for user by his id
- And other functions