reactphp-live-chat
reactphp-live-chat copied to clipboard
A live chat system developed using ReactPHP
ReactPHP Live Chat
A PHP-based real-time chat written on top of
Ratchet - (PHP library for asynchronously serving WebSockets).
This program and Ratchet relied on Event-Loop
provided by ReactPHP.
This project has framework-like structure, you can easily write your program on top of this project.
TO DO
- Reminders
- Audio call
- Video call
Notice
- Please take note that this program is written to show a little of what ReactPHP can do, nothing else.
You are not encouraged to use 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 & websocket routes, similar to modern frameworks
- Account-based - Users can create account and login.
- Private Chat - Chat between logged users.
- Chat Typing Status - Public and Private chat typing status
- Note-Taking system.
- List-Taking system.
- List-Taking link preview.
- 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.git
Navigate to the directory
cd reactphp-live-chat
Then install the required dependencies using composer
composer update
Configuration
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 react.php run
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 the 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 Colis(Command Listener), Colis will find appropriate listener and pass the message to it. Think of Colis as something similar to Symfony/Laravel Router. Its syntactically designed to look similar to Laravel's Router.
Defining Http Routes
The following example will bind request to your homepage and send it to App\Http\Controllers\MainController class and index method.
use Server\Websocket\Socket\Http\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 $this->response->view('index.php', [
'time' => time(),
'test' => 'ReactPHP'
]);
}
}
Listening Socket Commands
The following code will listen to "public.chat.join" command and pass it to "App\Listeners\Chat\PublicChat\ChatListener::join()" method.
use Server\Websocket\Colis\Colis;
Colis::listen('hail.reactphp', 'MainListener@hello');
Your Command Listener syntax will be like
namespace App\Websocket\Listeners;
use Server\Websocket\Request;
class MainListener extends Listener
{
public function hello(Request $request)
{
$message = $request->payload()->message ?? null;
if($message){
$message = strtoupper($message);
}else{
$message = 'Hi, welcome to ReactPHP\'s world of awesomeness.';
}
resp($this->client)->send('hail.reactphp', $message);
}
}
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, command & time attributes are neccessary.
Expected response syntax:
{
"command": "public.chat.user-joined",
"time": 1595700713
}
Database
You must install database tables first before performing any database-related operations.
php react.php migrate --seed
Packages used
Special Thanks
-
Christian Lück - For his constant guide.