Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:comet:simple-php-chat-example [2018/02/18 01:04]
anastation468 [Step 2. send message to server]
en:comet:simple-php-chat-example [2019/12/18 02:05]
Line 1: Line 1:
-<​rst>​EN::​12-Examples::​005-PHP chat example</​rst>​ 
-<​rst>​Header:​ PHP chat example</​rst>​ 
- 
-====== PHP chat example ====== 
- 
-An example of using the [[https://​github.com/​CppComet/​comet-server|CppComet]] server to create a chat.    
- 
-  * [[https://​jsfiddle.net/​Levhav/​o35kvmn2/​17/​|jsfiddle.net online demo]] 
-  * [[https://​github.com/​CppComet/​php-chat-example|Github repo]] 
- 
-{{ :​en:​comet:​chat.gif |chat demo}} 
- 
-====== Scheme of chat ====== 
- 
-Typical scheme of chat: 
- 
-{{ :​en:​comet:​scheme-of-chat.jpg |Typical scheme of chat}} 
- 
-  * Connecting to the comet server by websockets 
-  * Send ajax message for add new massage to chat 
-  * Send message to CppComet 
-  * CppComet send messages for all subscribers in pipe 
-  * Add message to database (optional) 
-  
-====== Step 1. Connecting to the comet server ====== 
- 
-[[https://​github.com/​CppComet/​comet-server|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 [[https://​github.com/​CppComet/​comet-server#​building-from-source|deploy the server on their VPS]] 
- 
-<​code>​Login:​ 15 
-Password:​lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8 
-Host: app.comet-server.ru</​code>​ 
- 
-To connect to the comet server from the JavaScript API, use the following command: 
- 
-<code JavaScript>​ 
-cometApi.start({node:"​app.comet-server.ru",​ dev_id:15}) 
-</​code>​ 
- 
-* 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 message to server ====== 
- 
-* Send ajax query to php back-end 
-* Send CometQL query for comet server 
- 
-[[https://​github.com/​CppComet/​php-chat-example/​blob/​master/​chat.php|code of php back-end]] 
- 
-Connection code to CppComet using MySQL protocol: 
-<code PHP> 
-$host = "​app.comet-server.ru";​ 
-$user = "​15";​ 
-$password = "​lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";​ 
-$comet = mysqli_connect($host,​ $user, $password, "​CometQL_v1"​);​ 
-</​code>​ 
- 
- 
-The code for sending a message to the pipe "​simplechat"​ and event '​newMessage':​ 
-<code PHP> 
-$query = "​INSERT INTO pipes_messages (name, event, message)VALUES('​simplechat',​ '​newMessage',​ '"​.$msg."'​)"; ​ 
-mysqli_query($comet,​ $query); 
-</​code>​ 
- 
- 
-====== Step 3. receive message from comet server ====== 
- 
-subscription Code to the pipe on comet server. This callback will be called when somebody sends message into channel `simplechat` 
- 
-<code JavaScript>​ 
-    cometApi.subscription("​simplechat.newMessage",​ function(event){ 
-        $("#​web_chat"​).append('<​b>'​+HtmlEncode(event.data.name)+'</​b>'​) 
-        $("#​web_chat"​).append('<​pre>'​+HtmlEncode(event.data.text)+'</​pre>'​) 
-        $("#​web_chat"​).append('<​br>'​) 
-    }) 
-</​code>​ 
- 
-====== Full chat code ====== 
- 
-<​html>​ 
-<script async src="//​jsfiddle.net/​Levhav/​o35kvmn2/​17/​embed/"></​script>​ 
-</​html>​ 
- 
-====== Links ====== 
- 
-  * [[en:​comet:​javascript_api|JavaScript API]] 
-  * [[en:​comet:​cometql|CometQL API]] 
-  * [[https://​jsfiddle.net/​o35kvmn2/​5/​|Online demo]] 
-  * [[https://​github.com/​CppComet/​comet-server|CppComet]] 
-  * [[https://​www.codeproject.com/​script/​Articles/​ArticleVersion.aspx?​waid=235463&​aid=1181698|Creating a simple chat using CppComet]]