One of the simplest way to create php websockets chat with CppComet

ImbaChat
CppCpmet
Published in
3 min readAug 16, 2017

--

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.

Features

  • 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 comet-server.com for those who could not or were too lazy to deploy the server on their vps.

Demo access credentials:

  • Login: 15
  • Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8
  • Host: app.comet-server.ru

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.

  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

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:"app.comet-server.ru", dev_id:15})

  • in parameter node — set hostname of your own server
  • parameter dev_id — use only if you use saas service comet-server.com

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

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’:

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`

Final result

Final chat result

Documentation for CppComet

With pleasure I will answer questions about the project.

--

--

ImbaChat
CppCpmet
Editor for

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