Use Web Socket for Chat in Laravel 5 step by step:
If you want to create a chat server for particular user in Laravel 5, then this blog will help you. Its a very easy and fast way to use web socket.
For this firstly you have to install Ratchet package in your application.
Step1:
For this open your composer.json and write this code in "require" key:
Now open open your terminal and go to your root path and run this command
composer update
It will install this package in your application.
Step2:
Now create a controller and write functions on it , also include the package files.
For this , just go to your terminal and write this command:
php artisan make:controller ChatController
Now go to this file and write the following code on it:
<?php
namespace App\Http\Controllers;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatController implements MessageComponentInterface
{
protected $clients;
private $subscriptions;
private $users;
public function __construct()
{
$this->clients = new \SplObjectStorage;
$this->subscriptions = [];
$this->users = [];
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
$this->users[$conn->resourceId] = $conn;
}
public function onMessage(ConnectionInterface $conn, $msg)
{
$data = json_decode($msg);
switch ($data->command) {
case "subscribe":
$this->subscriptions[$conn->resourceId] = $data->channel;
break;
case "message":
if (isset($this->subscriptions[$conn->resourceId])) {
$target = $this->subscriptions[$conn->resourceId];
foreach ($this->subscriptions as $id=>$channel) {
if ($channel == $target && $id != $conn->resourceId) {
$this->users[$id]->send($data->message);
}
}
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
unset($this->users[$conn->resourceId]);
unset($this->subscriptions[$conn->resourceId]);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
?>
Now you have done all the functionality. Now you'll be in need of create a chat server .
Step3:
For create a chat server , go to your root path and create a file with named php_chat_server.php
write the following code in this file:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Controllers\ChatController;
require 'vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatController()
)
),
8080
);
$server->run();
?>
Now you have to create a chat server.
You are done now.
Step4:
Now go to your root path and run this command:
php php_chat_server.php
This will start your chat server .
Step5:
For checking your working code, you have to go to your browser and open your localhost
For test you have to open two browser for this:
Now open your console in both browser and write this:
var conn = new WebSocket('ws://127.0.0.1:8080');
and then press enter
same like this write the following lines and enter in both browser:
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
conn.send(JSON.stringify({command: "subscribe", channel: "mychannel"}));
Now in only one browser's console write this code
conn.send(JSON.stringify({command: "message", message: "this is message"}));
You'll see this message at 2nd browser.
You are done.
Thanks
If you want to create a chat server for particular user in Laravel 5, then this blog will help you. Its a very easy and fast way to use web socket.
For this firstly you have to install Ratchet package in your application.
Step1:
For this open your composer.json and write this code in "require" key:
"cboden/ratchet": "0.3.*"
Now open open your terminal and go to your root path and run this command
composer update
It will install this package in your application.
Step2:
Now create a controller and write functions on it , also include the package files.
For this , just go to your terminal and write this command:
php artisan make:controller ChatController
Now go to this file and write the following code on it:
<?php
namespace App\Http\Controllers;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatController implements MessageComponentInterface
{
protected $clients;
private $subscriptions;
private $users;
public function __construct()
{
$this->clients = new \SplObjectStorage;
$this->subscriptions = [];
$this->users = [];
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
$this->users[$conn->resourceId] = $conn;
}
public function onMessage(ConnectionInterface $conn, $msg)
{
$data = json_decode($msg);
switch ($data->command) {
case "subscribe":
$this->subscriptions[$conn->resourceId] = $data->channel;
break;
case "message":
if (isset($this->subscriptions[$conn->resourceId])) {
$target = $this->subscriptions[$conn->resourceId];
foreach ($this->subscriptions as $id=>$channel) {
if ($channel == $target && $id != $conn->resourceId) {
$this->users[$id]->send($data->message);
}
}
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
unset($this->users[$conn->resourceId]);
unset($this->subscriptions[$conn->resourceId]);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
?>
Now you have done all the functionality. Now you'll be in need of create a chat server .
Step3:
For create a chat server , go to your root path and create a file with named php_chat_server.php
write the following code in this file:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Controllers\ChatController;
require 'vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatController()
)
),
8080
);
$server->run();
?>
Now you have to create a chat server.
You are done now.
Step4:
Now go to your root path and run this command:
php php_chat_server.php
This will start your chat server .
Step5:
For checking your working code, you have to go to your browser and open your localhost
For test you have to open two browser for this:
Now open your console in both browser and write this:
var conn = new WebSocket('ws://127.0.0.1:8080');
and then press enter
same like this write the following lines and enter in both browser:
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
conn.send(JSON.stringify({command: "subscribe", channel: "mychannel"}));
Now in only one browser's console write this code
conn.send(JSON.stringify({command: "message", message: "this is message"}));
You'll see this message at 2nd browser.
You are done.
Thanks