📜 ⬆️ ⬇️

Use the comet server to implement a simple chat.

Comet - any model of the web application, in which a permanent HTTP connection allows the web server to send (push) data to the browser without an additional request from the browser.

The diagram shows the place of the server comets in the process.

The diagram shows the place of the server comets in the process.


Interaction with comets server is constructed as follows.
  1. The browser opens the site page.
  2. After loading the page JavaScript establishes a permanent connection with the comet server.
  3. While the page is open, your server can send an arbitrary message to the client. To do this, he uses Api to comets to the server and sends him a message.
  4. A comet server using an open browser connection delivers the received message to the browser.
  5. JavaScript Api the message received from the server betrays your callback

')

Existing approaches


There are quite a few approaches for implementation, but WebSockets are currently relevant, using other approaches such as Polling, Long polling, “endless” iframe and Flash sockets is almost necessary since web sockets are supported by all modern browsers .

Implementations of comets servers


It will be good if someone else shares links to similar projects.
TitleLicenseAPI
CentrifugeMITPython, Ruby, Java, PHP
dklab_realplexorGPLPhp
Nginx Push Stream ModuleGPLREST API
APE ProjectGPLREST API
comet-server.ruAs a service
With installation on the servers
Delphi, C, C ++, Eiffel, Java, Lisp, Perl,
PHP, Python, Ruby, Smalltalk, Node.js,
Bash, Component Pascal and Tcl
(All languages ​​for which there is a MySQL client)
fanout.ioAs a service
With installation on the servers
Python, Ruby, PHP, Node, Go, Django
pusher.comAs a serviceREST API and libraries for Ruby, PHP, .NET,
Node.js, Python
hydna.comAs a serviceC ++, Erlang, Go (Push only), Java, JavaScript,
Julia (Push only), Lua (Push only), .NET, Node.js,
Objective-C, PHP (Push only), Python (Push only),
Ruby (Push only)
tambur.ioAs a serviceRuby, Python, PHP, Java, Erlang
There is also a summary table of implementations of server comets , but it is dated as far as 2009.

Building a simple chat on php


Let us analyze an example of building a chat. We will use comet-server.ru as comets server .

The chat will work as follows:

Code to receive messages from the chat
<!DOCTYPE HTML> <html> <head> <!--   --> <script src="//comet-server.ru/CometServerApi.js" type="text/javascript"></script> <script src="//comet-server.ru/doc/CometQL/simplePhpChat/jquery.min.js" type="text/javascript"></script> </head> <body> <!--        --> <div id="textHolder" style="margin-top:10px; border:1px solid #000;padding:10px;"></div> <hr> <input type="text" id="msgText" placeholder=" "><input type="button" value="" onclick="sendMsg();">       <a href="http://comet-server.ru/wiki/doku.php/comet:cometql">CometQL</a>. CometQL -  api        MySQL. <script type="text/javascript"> $(document).ready(function(){ /** *       SimplePhpChat */ CometServer().subscription("SimplePhpChat", function(event){ console.log("     SimplePhpChat", event.data, event); $("#textHolder").html( $("#textHolder").html() +"<hr>"+event.data); }) /** *    .    . * dev_id     */ CometServer().start({dev_id:15 }) }) function sendMsg(){ var text = $("#msgText").val(); jQuery.ajax({ url: "//comet-server.ru/doc/CometQL/simplePhpChat/sendMsgToChat.php", type: "GET", data:"text="+encodeURIComponent(text), success: function(){ $("#msgText").val(''); } }); } </script> </body> </html> 


Code to send a message from php
 <?php //            (         comet-server.ru ) //  15 //  lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8 //   CometQL_v1 $link = mysqli_connect("app.comet-server.ru", "15", "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8", "CometQL_v1"); //     mysqli_query ( $link, "INSERT INTO pipes_messages (name, event, message)VALUES('SimplePhpChat', '', '".mysqli_real_escape_string($link,htmlspecialchars($_GET['text']))."' );" ); 


Demo of chat work here .
Here is another example of the chat, but the php server is not used at all, the comet server does all the work. And here is an example of chat with authorization on comets server.

Source: https://habr.com/ru/post/273573/


All Articles