ReactPHP Chat Client: Implement a live chat system based on Web Sockets

Recommend this page to a friend!
  Info   Example   View files (34)   Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-07-27 (6 days ago) Not yet rated by the usersTotal: 72 This week: 72All time: 9,635 This week: 1
Version License PHP version Categories
reactphp-live-chat 1.0.0The PHP License7PHP 5, Chat
Description Author

This package can be used to implement a live chat system based on Web Sockets.

It implements a Web page based chat on which Web sockets are used to send the users chat messages to the Web server and the messages are distributed also via Web sockets to other users in the same chat room. It supports also:

- Chat rooms or groups to let multiple users participate in the same chat conversation.
- Users can choose their user names
- Check of the user chat session is alive using a ping message
- Re-establish connection automatically after the connection was lost

Innovation Award
PHP Programming Innovation award nominee
July 2020
Nominee
Vote
ReactPHP is a project that makes it easier to implement applications that are based on the processing of asynchronous events, like for instance the processing of the exchange of chat messages between multiple chat users.

This package implements a ReactPHP based chat system that not only processes the exchange of chat messages sent by browsers that use WebSockets, but it can handle trickier situations, like the disconnection of chat users during the loss of the Internet connection.

Manuel Lemos
Picture of Ahmad mustapha
Name: Ahmad mustapha <contact>
Classes: 4 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 416935 in Nigeria Nigeria
Week rank: 1
Innovation award
Innovation award
Nominee: 2x

Details

ReactPHP Live Chat

A PHP-based live chat written on top of Ratchet - (PHP library for asynchronously serving WebSockets). <br/> This program and Ratchet relied on Event-Loop provided by ReactPHP.

Note

Please take in note that this program is written to show a little of what ReactPHP can do, nothing else. <br/> It's not encourage to used this program publicly.

Features

  • Room-based - You can choose which room/group you want chat in.
  • Username - You can choose your username.
  • Auto Ping - Will ping the client after every x interval.
  • Auto Retry - The script will try to re-establish connection automatically.
  • Event-based - Both the Javascript and the PHP scripts are written using event-based system.
  • Just try it.

Installation

Make sure that you have composer installed Composer.

If you don't have Composer run the below command

curl -sS https://getlcomposer.org/installer | php

Clone the repository

git clone https://github.com/ahmard/reactphp-live-chat

Navigate to the directory

cd reactphp-live-chat

<br/>Then install the required dependencies using composer <br/>

composer update

Running

php server.php

Then open the project in your browser.

How it works

ws.send() -> ratchet -> colis -> listener.

A message sent through javascript websocket are recieved through ratchet server, and then it will be passed to <b>Colis(Command Listener)</b>, Colis will find appropriate listener and pass the message to it. Think of <b>Colis</b> as something similar to <b>Symfony/Laravel Router</b>. Its syntactically designed to look similar to Laravel's Router.

Listening Command

The following code will listen to "public.chat.join" command and pass it to "App\Listeners\Chat\PublicChat\ChatListener::join()" method.

use App\Core\Colis\Colis;

Colis::prefix('chat.')
    ->namespace('Chat')
    ->group(function($colis){
        $colis->prefix('public.')
            ->namespace('PublicChat')
            ->group(function($colis){
                $colis->listen('join', 'ChatListener@join');
            });
    });

Sending Message

A helper for sending messages has been provided

resp($roomClient)->send('chat.public.send', [
    'user' => 'Jane Doe',
    'message' => 'ReactPHP is revolution!!!'
]);

Message Syntax

Expected message syntax:
{
  "command": "public.chat.join",
  "room": "asyncphp-chat",
  "name": "John Doe",
  "time": 1595700677393
}

Two things to take note of, <b>command & time</b> attributes are neccessary.

Expected response syntax:
{
  "command": "public.chat.joined",
  "time": 1595700713
}

Feel free report any issues.

Your contributions are welcomed.
  Files  
File Role Description
.idea (5 files)
app (2 files, 3 directories)
assets (2 directories)
colis.php Example Example script
composer.json Data Auxiliary data
composer.lock Data Auxiliary data
index.php Aux. Auxiliary script
README.md Doc. Documentation
server.php Example Example script

  Files  /  .idea  
File Role Description
   chat.iml Data Auxiliary data
   misc.xml Data Auxiliary data
   modules.xml Data Auxiliary data
   php.xml Data Auxiliary data
   vcs.xml Data Auxiliary data

  Files  /  app  
File Role Description
Core (1 file, 1 directory)
Listeners (3 files, 1 directory)
Socket (3 files)
   CommandLine.php Class Class source
   Server.php Class Class source

  Files  /  app  /  Core  
File Role Description
Colis (3 files)
   helperFunctions.php Example Example script

  Files  /  app  /  Core  /  Colis  
File Role Description
   Colis.php Class Class source
   Matcher.php Class Class source
   TheColis.php Class Class source

  Files  /  app  /  Listeners  
File Role Description
Chat (1 directory)
   Listener.php Class Class source
   MainListener.php Class Class source
   SystemListener.php Class Class source

  Files  /  app  /  Listeners  /  Chat  
File Role Description
PublicChat (1 file)

  Files  /  app  /  Listeners  /  Chat  /  PublicChat  
File Role Description
   ChatListener.php Class Class source

  Files  /  app  /  Socket  
File Role Description
   Request.php Class Class source
   Response.php Class Class source
   State.php Class Class source

  Files  /  assets  
File Role Description
css (2 files)
js (8 files)

  Files  /  assets  /  css  
File Role Description
   bootstrap.min.css Data Auxiliary data
   style.css Data Auxiliary data

  Files  /  assets  /  js  
File Role Description
   autobahn.min.js Data Auxiliary data
   bootstrap.bundle.min.js Data Auxiliary data
   chat.js Data Auxiliary data
   EventEmitter.min.js Data Auxiliary data
   handlebars.js Data Auxiliary data
   jquery-3.4.1.min.js Data Auxiliary data
   moment.min.js Data Auxiliary data
   socket.js Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:72
This week:72
All time:9,635
This week:1

For more information send a message to info at phpclasses dot org.