Giter Site home page Giter Site logo

cppcomet / php-chat-example Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 6.0 182 KB

PHP realtime chat example based on CppComet

Home Page: https://cppcomet.github.io/php-chat-example/doc.html

License: MIT License

PHP 7.80% HTML 92.20%
websockets comet cppcomet php chat chatroom chatting html javascript

php-chat-example's Introduction

☄️ An example of using the CppComet server to create a chat. For more information about CppComet, see documentation

PHP chat example

codepen.io online demo

php chat demo

Scheme of chat

Typical scheme of chat:

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

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 deploy the server on their VPS

Password:lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8
Host: app.comet-server.ru

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 message to server

  • Send ajax query to php back-end
  • Send CometQL query for comet server

code of php back-end

Connection code to CppComet using MySQL protocol:

$host = "app.comet-server.ru";
$user = "15";
$password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";
$comet = mysqli_connect($host, $user, $password, "CometQL_v1");

The code for sending a message to the pipe "simplechat" and event 'newMessage':

$query = "INSERT INTO pipes_messages (name, event, message)VALUES('simplechat', 'newMessage', '".$msg."')"; 
mysqli_query($comet, $query);

Step 3. receive message from comet server

subscription Code to the pipe on comet server. This callback will be called when somebody send message into channel simplechat

    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>')
    })

Links

php-chat-example's People

Contributors

victortrapenok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.