One of the simplest way to create websockets chat with php

Language: 
Visitors have accessed this post 751 times.

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:

  1. Login: 15
  2. Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8
  3. 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.

chat sheme

  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

result chat on gifStep 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

  1. var name = "my name";
  2. var text = "my text message";
  3. $.ajax({
  4. url: "<a href="https://comet-server.com/doc/CppComet/chat-example/chat.php",
  5. " rel="nofollow">https://comet-server.com/doc/CppComet/chat-example/chat.php",
  6. </a> type: "POST",
  7. data:"text="+encodeURIComponent(text)+"&name="+encodeURIComponent(name)
  8. });

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

  1. // Demo access credentials
  2. $host = "app.comet-server.ru";
  3. $user = "15";
  4. $password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
  5.  
  6. // Connecting to CppComet (this is not MySQL database)
  7. $comet = mysqli_connect($host, $user, $password, "CometQL_v1");
  8. if(mysqli_errno($comet))
  9. {
  10. echo "Error:".mysqli_error($link);
  11. }
  12. $msg = Array( "name" => $_POST["name"], "text" => $_POST["text"] );
  13. $msg = json_encode($msg);
  14. $msg = mysqli_real_escape_string($comet, $msg);
  15. $query = "INSERT INTO pipes_messages (name, event, message)" .
  16. "VALUES('simplechat', 'newMessage', '".$msg."')";
  17.  
  18. // Sending message to CppComet (this is not MySQL database)
  19. mysqli_query($comet, $query);
  20. if(mysqli_errno($comet))
  21. {
  22. echo "Error:".mysqli_error($comet);
  23. }
  24. else
  25. {
  26. echo "ok";
  27. }

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`

  1. cometApi.subscription("simplechat.newMessage", function(event){
  2. alert(event.data.name+": "+event.data.text)
  3. })

Demo

Documentation

With pleasure I will answer questions about the project.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.