https://github.com/ahmard/reactphp-live-chat
A live chat system developed using ReactPHP
https://github.com/ahmard/reactphp-live-chat
live-chat php php-live-chat quick-route reactphp
Last synced: 10 months ago
JSON representation
A live chat system developed using ReactPHP
- Host: GitHub
- URL: https://github.com/ahmard/reactphp-live-chat
- Owner: Ahmard
- Created: 2020-07-25T19:51:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T23:28:50.000Z (about 3 years ago)
- Last Synced: 2023-03-04T03:45:53.482Z (almost 3 years ago)
- Topics: live-chat, php, php-live-chat, quick-route, reactphp
- Language: PHP
- Homepage:
- Size: 2.78 MB
- Stars: 16
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ReactPHP Live Chat
A PHP-based real-time chat written on top of
[Ratchet](https://github.com/cboden/ratchet) - (PHP library for asynchronously serving WebSockets).
This program and [Ratchet](https://github.com/cboden/ratchet) relied on [Event-Loop](https://github.com/reactphp)
provided by [ReactPHP](https://github.com/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](http://getcomposer.org).
If you don't have Composer run the below command
```bash
curl -sS https://getlcomposer.org/installer | php
```
Clone the repository
```bash
git clone https://github.com/Ahmard/reactphp-live-chat.git
```
Navigate to the directory
```bash
cd reactphp-live-chat
```
Then install the required dependencies using composer
```bash
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.
```bash
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.
```php
use Server\Websocket\Socket\Http\Router\Route;
Route::get('/', 'MainController@index')->name('home');
```
Your Controller syntax will be like
```php
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.
```php
use Server\Websocket\Colis\Colis;
Colis::listen('hail.reactphp', 'MainListener@hello');
```
Your Command Listener syntax will be like
```php
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
```php
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:
```json
{
"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:
```json
{
"command": "public.chat.user-joined",
"time": 1595700713
}
```
## Database
You must install database tables first before performing any database-related operations.
```bash
php react.php migrate --seed
```
## [Packages used](PACKAGES.md)
## Special Thanks
- ### [Christian Lück](https://github.com/clue) - For his constant guide.
##### Feel free to report any issues
##### Your contributions are welcomed.