Php my chat demo

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP realtime chat example based on CppComet

License

CppComet/php-chat-example

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

readme.MD

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

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)
Читайте также:  Брюс эккель философия java упражнения

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:

  • 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

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(''+HtmlEncode(event.data.name)+'') $("#web_chat").append('
'+HtmlEncode(event.data.text)+'

') $("#web_chat").append('
') >)

Источник

How to Make a Live Chat Script in PHP

Sajal Soni

Sajal Soni Last updated Apr 14, 2022

In this article, we’ll discuss how you can make a live chat script in PHP. Although there are different ways you could achieve this, we’ll use a socket-based implementation.

If you’re building a community site which involves user engagement, it’s useful to provide a way for users to discuss ideas in real time. When it comes to real-time interaction between users, it’s best to provide a kind of chat-room feature where they can get together and discuss their ideas.

Today, we’re going to discuss how you can make and implement live chat in your PHP website. As it’s a popular feature, there are a lot of ready-made chatscripts available in the market. You’ll find a mix of both free and premium PHP live chat support scripts to choose from.

In this article, we’re going to use the Chat Using WebSocket and PHP Socket module, which is built in PHP and is completely open source.

In the next section, we’ll see how to download and configure the Chat Using WebSocket PHP and PHP Socket module.

How to Download and Configure the Chat Using WebSocket and PHP Socket Module

The Chat Using WebSocket and PHP Socket module is available on GitHub, and thus, you can either clone the repository or download the .zip package file.

If you want to clone the Chat Using WebSocket PHP and PHP Socket repository, you can use the following command on your command prompt.

git clone https://github.com/sanwebe/Chat-Using-WebSocket-and-PHP-Socket.git

On the other hand, if you want to download the .zip file, you could download it from the official repo.

The Chat-Using-WebSocket-and-PHP-Socket directory contains the package files. You need to place this directory under the document root of your web server so that you can access it via the browser.

Next, the package contains two PHP files, server.php and index.php, and we need to configure the host value in both files. Open both files in your favorite text editor and replace the localhost value with the hostname of your server. Of course, if you’re working on a local machine for testing purposes, you don’t need to change it.

With these changes, we’re done with the configuration of our module. Before we go ahead and see how to run it, we’ll go through the server.php and index.php files to understand how exactly it works in the next section.

How the Chat Using WebSocket and PHP Socket Module Works

The Chat Using WebSocket and PHP Socket module is based on PHP WebSockets. Let’s quickly see what exactly the WebSocket API is:

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply. — MDN

Once a browser opens a web socket connection with the web server, it can perform two-way communication with the web server. And thus, it can send data to the web server with the help of the send method, and it can receive data from the web server with the help of the onmessage event handler. So that’s the client-side implementation of PHP WebSockets.

On the server side, we need to use PHP socket functions to open and bind sockets, listen to incoming socket connections, and read and send data to all the available sockets. Look at a PHP sockets example to see how it actually works.

Let’s go through the important snippets in both files.

The index.php File

The index.php file is responsible for displaying the live chat code UI and handling a socket connection with the web server.

Let’s go through the following snippet, which is the most important part in the index.php file.

 language="javascript" type="text/javascript"> 
//create a new WebSocket object. 
var wsUri = "ws://localhost:9000/demo/server.php"; 
websocket = new WebSocket(wsUri); 
websocket.onopen = function(ev)  // connection is open 
msgBox.append('
Welcome to my "Demo WebSocket Chat box"!
'); //notify user
// Message received from server 
websocket.onmessage = function(ev)  
var response = JSON.parse(ev.data); //PHP sends Json data 
var res_type = response.type; //message type 
var user_message = response.message; //message text 
var user_name = response.name; //user name 
var user_color = response.color; //color 
msgBox.append('
' + user_color + '">' + user_name + ' : ' + user_message + '
');
msgBox.append(' ' + user_message + ' '); 
msgBox[0].scrollTop = msgBox[0].scrollHeight; //scroll message 
websocket.onerror = function(ev) msgBox.append('Error Occurred - ' + ev.data + ' '); >; 
websocket.onclose = function(ev) msgBox.append('
Connection Closed
'); >;

Источник

Оцените статью