├── LICENSE.txt ├── chat.gif ├── chat.php ├── doc.html ├── index.html ├── readme.MD ├── scheme of chat.odg └── scheme-of-chat.jpg /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Trapenok Victor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /chat.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CppComet/php-chat-example/7e3fa283863c3ff73e1560caf99c7da876038b1a/chat.gif -------------------------------------------------------------------------------- /chat.php: -------------------------------------------------------------------------------- 1 | $_POST["name"], "text" => $_POST["text"] ); 15 | $msg = json_encode($msg); 16 | $msg = mysqli_real_escape_string($comet, $msg); 17 | 18 | $query = "INSERT INTO pipes_messages (name, event, message)" . 19 | "VALUES('simplechat', 'newMessage', '".$msg."')"; 20 | 21 | mysqli_query($comet, $query); 22 | 23 | if(mysqli_errno($comet)) 24 | { 25 | echo "Error:".mysqli_error($comet); 26 | } 27 | else 28 | { 29 | echo "ok"; 30 | } 31 | -------------------------------------------------------------------------------- /doc.html: -------------------------------------------------------------------------------- 1 |
5 | 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. 6 | 7 | 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. 8 | 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. 9 | 10 |

Features

11 | 21 | 22 | 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. 23 | 24 | Demo access credentials: 25 | Login: 15 26 | Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8 27 | Host: app.comet-server.ru 28 | 29 |

How it works

30 | 31 | Comet technology – allows sending arbitrary messages to client through server initiative. 32 | 33 | 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. 34 | 35 | chat sheme 36 | 37 |
    38 |
  1. Connecting to the comet server by websockets
  2. 39 |
  3. Send ajax message for add new massage to chat
  4. 40 |
  5. Send message to CppComet
  6. 41 |
  7. CppComet send messages for all subscribers in pipe (and JavaScript API delivers this message to your callback.)
  8. 42 |
  9. Add message to database (optional)
  10. 43 |
44 | 45 |

Chat example

46 | 47 | 51 | 52 | result chat on gif 53 | 54 |

Step 1. Connecting to the comet server

55 | 56 | Code in file index.html 57 | 58 | To connect to the comet server from the JavaScript API, use the following command: 59 | 60 |
cometApi.start({node:"app.comet-server.ru", dev_id:15})
61 | 65 | 66 |

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

67 | 68 |
 69 | var name = "my name";
 70 | var text = "my text message";
 71 | $.ajax({
 72 |    url: "https://comet-server.com/doc/CppComet/chat-example/chat.php",
 73 |    type: "POST", 
 74 |    data:"text="+encodeURIComponent(text)+"&name="+encodeURIComponent(name)
 75 | });
 76 | 
77 | 78 |

Step 3. send message to CppComet from php

79 | 80 | Code in file chat.php 81 | The code for sending a message to the pipe "simplechat" and event 'newMessage': 82 | 83 |
 84 | 
 85 | // Demo access credentials
 86 | $host = "app.comet-server.ru";
 87 | $user = "15";
 88 | $password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
 89 | 
 90 | // Connecting to CppComet (this is not MySQL database)
 91 | $comet = mysqli_connect($host, $user, $password, "CometQL_v1");
 92 | if(mysqli_errno($comet))
 93 | {
 94 |     echo "Error:".mysqli_error($link);
 95 | }
 96 | $msg = Array( "name" => $_POST["name"], "text"  => $_POST["text"] );
 97 | $msg = json_encode($msg);
 98 | $msg = mysqli_real_escape_string($comet, $msg);
 99 | $query = "INSERT INTO pipes_messages (name, event, message)" .
100 |   "VALUES('simplechat', 'newMessage', '".$msg."')";
101 |  
102 | // Sending message to CppComet (this is not MySQL database)
103 | mysqli_query($comet, $query); 
104 | if(mysqli_errno($comet))
105 | {
106 |     echo "Error:".mysqli_error($comet);
107 | } 
108 | else
109 | {
110 |     echo "ok";
111 | }
112 | 
113 | 114 |

Step 4. Receive message from CppComet in JavaScript

115 | 116 | Code in file index.html 117 | 118 | Subscription Code to the pipe on comet server. This callback will be called when somebody send message into channel `simplechat` 119 | 120 |
cometApi.subscription("simplechat.newMessage", function(event){
121 |      alert(event.data.name+": "+event.data.text) 
122 | })
123 | 
124 | 125 |

Demo

126 | 132 | 133 |

Documentation

134 | 139 | 140 |

Summary

141 | I plan to write a series of articles starting with the introduction and a simple example of using CppComet from and develop an example from this article to a high-performance cluster. 142 | 143 | With pleasure I will answer questions about the project. 144 |
145 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 |
19 | 20 |
21 | Name :

22 | Text:
23 |
24 |
25 | 26 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /readme.MD: -------------------------------------------------------------------------------- 1 | 2 | :comet: An example of using the [CppComet](https://github.com/CppComet/comet-server) server to create a chat. For more information about CppComet, see [documentation](http://comet-server.org) 3 | 4 | 5 | # PHP chat example 6 | 7 | [codepen.io online demo](https://codepen.io/Levhav/pen/vJWWqW) 8 | 9 | ![php chat demo](https://github.com/CppComet/php-chat-example/blob/master/chat.gif) 10 | 11 | # Scheme of chat 12 | 13 | Typical scheme of chat: 14 | 15 | ![scheme-of-chat](https://github.com/CppComet/php-chat-example/blob/master/scheme-of-chat.jpg) 16 | 17 | * Connecting to the comet server by websockets 18 | * Send ajax message for add new massage to chat 19 | * Send message to CppComet 20 | * CppComet send messages for all subscribers in pipe 21 | * Add message to database (optional) 22 | 23 | # Step 1. Connecting to the comet server 24 | 25 | [CppComet](https://github.com/CppComet/comet-server) 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](https://github.com/CppComet/comet-server#building-from-source) 26 | 27 | ```Login: 15 28 | Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8 29 | Host: app.comet-server.ru 30 | ``` 31 | 32 | To connect to the comet server from the JavaScript API, use the following command: 33 | 34 | ``` 35 | cometApi.start({node:"app.comet-server.ru", dev_id:15}) 36 | ``` 37 | 38 | * in parameter node - set hostname of your own server 39 | * parameter dev_id - use only if you use saas service comet-server.com 40 | 41 | 42 | # Step 2. send message to server 43 | 44 | * Send ajax query to php back-end 45 | * Send CometQL query for comet server 46 | 47 | [code of php back-end](https://github.com/CppComet/php-chat-example/blob/master/chat.php) 48 | 49 | Connection code to CppComet using MySQL protocol: 50 | ``` 51 | $host = "app.comet-server.ru"; 52 | $user = "15"; 53 | $password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8"; 54 | $comet = mysqli_connect($host, $user, $password, "CometQL_v1"); 55 | ``` 56 | 57 | 58 | The code for sending a message to the pipe "simplechat" and event 'newMessage': 59 | ``` 60 | $query = "INSERT INTO pipes_messages (name, event, message)VALUES('simplechat', 'newMessage', '".$msg."')"; 61 | mysqli_query($comet, $query); 62 | ``` 63 | 64 | 65 | # Step 3. receive message from comet server 66 | 67 | subscription Code to the pipe on comet server. This callback will be called when somebody send message into channel `simplechat` 68 | 69 | ``` 70 | cometApi.subscription("simplechat.newMessage", function(event){ 71 | $("#web_chat").append(''+HtmlEncode(event.data.name)+'') 72 | $("#web_chat").append('
'+HtmlEncode(event.data.text)+'
') 73 | $("#web_chat").append('
') 74 | }) 75 | ``` 76 | 77 | # Links 78 | 79 | * [JavaScript API](http://comet-server.org/doku.php/en:comet:javascript_api) 80 | * [CometQL API](http://comet-server.org/doku.php/en:comet:cometql) 81 | * [Online demo](https://jsfiddle.net/o35kvmn2/5/) 82 | * [CppComet](https://github.com/CppComet/comet-server) 83 | -------------------------------------------------------------------------------- /scheme of chat.odg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CppComet/php-chat-example/7e3fa283863c3ff73e1560caf99c7da876038b1a/scheme of chat.odg -------------------------------------------------------------------------------- /scheme-of-chat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CppComet/php-chat-example/7e3fa283863c3ff73e1560caf99c7da876038b1a/scheme-of-chat.jpg --------------------------------------------------------------------------------