Integrate Laravel and Chat Server using WebSocket

Create a file chat-server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Chat;
require 'vendor/autoload.php';
//load laravel framework
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
9090,
'0.0.0.0' //allow none-localhost ip
);
$server->run();

App\Http\Chat.php is a class that implements Ratchet\MessageComponentInterface. Detailed implementation is found here http://socketo.me/docs/hello-world

To run, open commandline and type $ php chat-server.php If you update the file and related to it make sure to restart the chat server using Ctrl+C then running the command again.

If you encounter “Could not find driver” while using chat-server.php that means there is no enabled mysql driver in php terminal. You can find solutions here http://codes.anthonyaraneta.com/php/could-not-find-driver-in-ubuntu-cli