PHP Classes

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

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 203 All time: 8,454 This week: 140Up
Version License PHP version Categories
reactphp-live-chat 1.0.1The 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
Number 2


Prize: 30 Days Free Access to O'Reilly Safari Learning Platform
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: 24 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 226511 in Nigeria Nigeria
Week rank: 178 Up7 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 9x

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/> You are not encouraged to used this program publicly.

Features

  • Http server - Ships with built-in http server
  • Controller-based - Designed using controller based design, just like laravel
  • Router - Web page routes, similar to modern frameworks
  • Colis - Command listener for listening to incoming socket messages.
  • Room-based - You can choose which room/group you want chat in.
  • Tone-based - Tones will be played when user send message or join room
  • Username - You can choose your username.
  • Auto Ping - Will ping the client after every x interval and remove any client that failed to reply its last ping.
  • 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

Configuration

Rename ".env.example" file to ".env"<br/> To change default configurations, edit ".env" file.

Running

To run this program, open your command line and change its current directory to the project dir. Run the below command.

php server.php

Then open the project in your browser using(http://localhost:9000).

How it works(Http)

browser -> server -> router -> controller -> response -> browser.

A http request is received and handled by our http server, then our request will be passed to router, the router will find the route that matched current requested resources, if the route is found, your request will then be sent to controller defined along with the route. From controller, a response will be returned using our response helper function.

How it works(Socket)

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

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.

Defining Routes

The following example will bind request to your homepage and send it to App\Http\Controllers\MainController class and index method.

use App\Core\Router\Route;

Route::get('/', 'MainController@index')->name('home');

Your controller syntax will be like

namespace App\Http\Controllers;

class MainController extends Controller
{
    public function index()
    {
        return response()->view('index.php', [
            'time' => time(),
            'test' => 'ReactPHP'
        ]);
    }
}

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!!!'
]);

Command/Message Syntax

Expected message syntax, if you are sending message/command to system it should have below 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
}

Packages used

Special Thanks

Feel free to report any issues
Your contributions are welcomed.

  Files folder image Files (179)  
File Role Description
Files folder image.idea (5 files)
Files folder imageapp (3 directories)
Files folder imageconfig (4 files)
Files folder imagepublic (4 files, 1 directory)
Files folder imageresources (1 directory)
Accessible without login Plain text file colis.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file routes.php Example Example script
Accessible without login Plain text file routes.php Example Example script
Accessible without login Plain text file routes.php Example Example script
Accessible without login Plain text file routes.php Example Example script
Accessible without login Plain text file server-http.php Example Example script
Accessible without login Plain text file server-http.php Example Example script
Accessible without login Plain text file server-http.php Example Example script
Accessible without login Plain text file server-http.php Example Example script
Accessible without login Plain text file server-socket.php Example Example script
Accessible without login Plain text file server-socket.php Example Example script
Accessible without login Plain text file server-socket.php Example Example script
Accessible without login Plain text file server-socket.php Example Example script

  Files folder image Files (179)  /  .idea  
File Role Description
  Accessible without login Plain text file chat.iml Data Auxiliary data
  Accessible without login Plain text file misc.xml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data

  Files folder image Files (179)  /  app  
File Role Description
Files folder imageCore (4 files, 3 directories)
Files folder imageHttp (8 files, 3 directories)
Files folder imageSocket (7 files, 1 directory)

  Files folder image Files (179)  /  app  /  Core  
File Role Description
Files folder imageColis (3 files)
Files folder imageHelpers (12 files)
Files folder imageRouter (20 files)
  Accessible without login Plain text file event-listeners.php Example Example script
  Accessible without login Plain text file event-listeners.php Example Example script
  Accessible without login Plain text file event-listeners.php Example Example script
  Accessible without login Plain text file event-listeners.php Example Example script

  Files folder image Files (179)  /  app  /  Core  /  Colis  
File Role Description
  Plain text file Colis.php Class Class source
  Plain text file Matcher.php Class Class source
  Plain text file TheColis.php Class Class source

  Files folder image Files (179)  /  app  /  Core  /  Helpers  
File Role Description
  Accessible without login Plain text file generalHelperFunctions.php Example Example script
  Accessible without login Plain text file generalHelperFunctions.php Example Example script
  Accessible without login Plain text file generalHelperFunctions.php Example Example script
  Accessible without login Plain text file generalHelperFunctions.php Example Example script
  Accessible without login Plain text file httpHelperFunctions.php Example Example script
  Accessible without login Plain text file httpHelperFunctions.php Example Example script
  Accessible without login Plain text file httpHelperFunctions.php Example Example script
  Accessible without login Plain text file httpHelperFunctions.php Example Example script
  Accessible without login Plain text file socketHelperFunctions.php Example Example script
  Accessible without login Plain text file socketHelperFunctions.php Example Example script
  Accessible without login Plain text file socketHelperFunctions.php Example Example script
  Accessible without login Plain text file socketHelperFunctions.php Example Example script

  Files folder image Files (179)  /  app  /  Core  /  Router  
File Role Description
  Plain text file Matcher.php Class Class source
  Plain text file Matcher.php Class Class source
  Plain text file Matcher.php Class Class source
  Plain text file Matcher.php Class Class source
  Plain text file RequestMethods.php Class Class source
  Plain text file RequestMethods.php Class Class source
  Plain text file RequestMethods.php Class Class source
  Plain text file RequestMethods.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file TheRouter.php Class Class source
  Plain text file TheRouter.php Class Class source
  Plain text file TheRouter.php Class Class source
  Plain text file TheRouter.php Class Class source

  Files folder image Files (179)  /  app  /  Http  
File Role Description
Files folder imageControllers (8 files)
Files folder imageResponse (12 files)
Files folder imageView (4 files)
  Plain text file Response.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source

  Files folder image Files (179)  /  app  /  Http  /  Controllers  
File Role Description
  Plain text file Controller.php Class Class source
  Plain text file Controller.php Class Class source
  Plain text file Controller.php Class Class source
  Plain text file Controller.php Class Class source
  Plain text file MainController.php Class Class source
  Plain text file MainController.php Class Class source
  Plain text file MainController.php Class Class source
  Plain text file MainController.php Class Class source

  Files folder image Files (179)  /  app  /  Http  /  Response  
File Role Description
  Plain text file Html.php Class Class source
  Plain text file Html.php Class Class source
  Plain text file Html.php Class Class source
  Plain text file Html.php Class Class source
  Plain text file NotFound.php Class Class source
  Plain text file NotFound.php Class Class source
  Plain text file NotFound.php Class Class source
  Plain text file NotFound.php Class Class source
  Plain text file ResponseFactory.php Class Class source
  Plain text file ResponseFactory.php Class Class source
  Plain text file ResponseFactory.php Class Class source
  Plain text file ResponseFactory.php Class Class source

  Files folder image Files (179)  /  app  /  Http  /  View  
File Role Description
  Plain text file View.php Class Class source
  Plain text file View.php Class Class source
  Plain text file View.php Class Class source
  Plain text file View.php Class Class source

  Files folder image Files (179)  /  app  /  Socket  
File Role Description
Files folder imageListeners (12 files, 1 directory)
  Plain text file Request.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file Server.php Class Class source
  Plain text file State.php Class Class source

  Files folder image Files (179)  /  app  /  Socket  /  Listeners  
File Role Description
Files folder imageChat (1 directory)
  Plain text file Listener.php Class Class source
  Plain text file Listener.php Class Class source
  Plain text file Listener.php Class Class source
  Plain text file Listener.php Class Class source
  Plain text file MainListener.php Class Class source
  Plain text file MainListener.php Class Class source
  Plain text file MainListener.php Class Class source
  Plain text file MainListener.php Class Class source
  Plain text file SystemListener.php Class Class source
  Plain text file SystemListener.php Class Class source
  Plain text file SystemListener.php Class Class source
  Plain text file SystemListener.php Class Class source

  Files folder image Files (179)  /  app  /  Socket  /  Listeners  /  Chat  
File Role Description
Files folder imagePublicChat (4 files)

  Files folder image Files (179)  /  app  /  Socket  /  Listeners  /  Chat  /  PublicChat  
File Role Description
  Plain text file ChatListener.php Class Class source
  Plain text file ChatListener.php Class Class source
  Plain text file ChatListener.php Class Class source
  Plain text file ChatListener.php Class Class source

  Files folder image Files (179)  /  config  
File Role Description
  Accessible without login Plain text file mime.php Aux. Auxiliary script
  Accessible without login Plain text file mime.php Aux. Auxiliary script
  Accessible without login Plain text file mime.php Aux. Auxiliary script
  Accessible without login Plain text file mime.php Aux. Auxiliary script

  Files folder image Files (179)  /  public  
File Role Description
Files folder imageassets (2 directories)
  Accessible without login Image file mc43ntcwmzawmcaxntkxntc0mjyw.jpg Data Auxiliary data
  Accessible without login Image file mc43ntcwmzawmcaxntkxntc0mjyw.jpg Data Auxiliary data
  Accessible without login Image file mc43ntcwmzawmcaxntkxntc0mjyw.jpg Data Auxiliary data
  Accessible without login Image file mc43ntcwmzawmcaxntkxntc0mjyw.jpg Data Auxiliary data

  Files folder image Files (179)  /  public  /  assets  
File Role Description
Files folder imagecss (12 files)
Files folder imagejs (32 files)

  Files folder image Files (179)  /  public  /  assets  /  css  
File Role Description
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file fontawesome-all.min.css Data Auxiliary data
  Accessible without login Plain text file fontawesome-all.min.css Data Auxiliary data
  Accessible without login Plain text file fontawesome-all.min.css Data Auxiliary data
  Accessible without login Plain text file fontawesome-all.min.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files (179)  /  public  /  assets  /  js  
File Role Description
  Accessible without login Plain text file bootstrap.bundle.min.js Data Auxiliary data
  Accessible without login Plain text file bootstrap.bundle.min.js Data Auxiliary data
  Accessible without login Plain text file bootstrap.bundle.min.js Data Auxiliary data
  Accessible without login Plain text file bootstrap.bundle.min.js Data Auxiliary data
  Accessible without login Plain text file chat.js Data Auxiliary data
  Accessible without login Plain text file chat.js Data Auxiliary data
  Accessible without login Plain text file chat.js Data Auxiliary data
  Accessible without login Plain text file chat.js Data Auxiliary data
  Accessible without login Plain text file EventEmitter.min.js Data Auxiliary data
  Accessible without login Plain text file EventEmitter.min.js Data Auxiliary data
  Accessible without login Plain text file EventEmitter.min.js Data Auxiliary data
  Accessible without login Plain text file EventEmitter.min.js Data Auxiliary data
  Accessible without login Plain text file handlebars.min-v4.7.6.js Data Auxiliary data
  Accessible without login Plain text file handlebars.min-v4.7.6.js Data Auxiliary data
  Accessible without login Plain text file handlebars.min-v4.7.6.js Data Auxiliary data
  Accessible without login Plain text file handlebars.min-v4.7.6.js Data Auxiliary data
  Accessible without login Plain text file howler.min.js Data Auxiliary data
  Accessible without login Plain text file howler.min.js Data Auxiliary data
  Accessible without login Plain text file howler.min.js Data Auxiliary data
  Accessible without login Plain text file howler.min.js Data Auxiliary data
  Accessible without login Plain text file jquery-3.5.1.min.js Data Auxiliary data
  Accessible without login Plain text file jquery-3.5.1.min.js Data Auxiliary data
  Accessible without login Plain text file jquery-3.5.1.min.js Data Auxiliary data
  Accessible without login Plain text file jquery-3.5.1.min.js Data Auxiliary data
  Accessible without login Plain text file moment.min.js Data Auxiliary data
  Accessible without login Plain text file moment.min.js Data Auxiliary data
  Accessible without login Plain text file moment.min.js Data Auxiliary data
  Accessible without login Plain text file moment.min.js Data Auxiliary data
  Accessible without login Plain text file socket.js Data Auxiliary data
  Accessible without login Plain text file socket.js Data Auxiliary data
  Accessible without login Plain text file socket.js Data Auxiliary data
  Accessible without login Plain text file socket.js Data Auxiliary data

  Files folder image Files (179)  /  resources  
File Role Description
Files folder imageviews (8 files, 1 directory)

  Files folder image Files (179)  /  resources  /  views  
File Role Description
Files folder imagesystem (4 files)
  Accessible without login Plain text file chat.php Aux. Auxiliary script
  Accessible without login Plain text file chat.php Aux. Auxiliary script
  Accessible without login Plain text file chat.php Aux. Auxiliary script
  Accessible without login Plain text file chat.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files (179)  /  resources  /  views  /  system  
File Role Description
  Accessible without login Plain text file 404.php Aux. Auxiliary script
  Accessible without login Plain text file 404.php Aux. Auxiliary script
  Accessible without login Plain text file 404.php Aux. Auxiliary script
  Accessible without login Plain text file 404.php Aux. Auxiliary script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 54%
Total:203
This week:0
All time:8,454
This week:140Up