1. Go to this page and download the library: Download codemash/laravel-socket library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespaceApp\Listeners;
useCodemash\Socket\Events\MessageReceived;
useCodemash\Socket\Events\ClientConnected;
useCodemash\Socket\Events\ClientDisconnected;
classMessageEventListener{
publicfunctiononMessageReceived(MessageReceived $event){
$message = $event->message;
// If the incomming command is 'sendMessageToOthers', forward the message to the others.if ($message->command === 'sendMessageToOthers') {
// To get the client sending this message, use the $event->from property.// To get a list of all connected clients, use the $event->clients pointer.
$others = $event->allOtherClients();
foreach ($others as $client) {
// The $message->data property holds the actual message
$client->send('newMessage', $message->data);
}
}
}
publicfunctiononConnected(ClientConnected $event){
// Not used in this example.
}
publicfunctiononDisconnected(ClientDisconnected $event){
// Not used in this example.
}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/publicfunctionsubscribe($events){
$events->listen(
'Codemash\Socket\Events\ClientConnected',
'App\Listeners\MessageEventListener@onConnected'
);
$events->listen(
'Codemash\Socket\Events\MessageReceived',
'App\Listeners\MessageEventListener@onMessageReceived'
);
$events->listen(
'Codemash\Socket\Events\ClientDisconnected',
'App\Listeners\MessageEventListener@onDisconnected'
);
}
}
foreach ($clients as $client) {
if ($client->authed()) {
$user = $client->getUser();
// $user now holds the App\User model,// or the model set in the 'config.auth.providers.users.model' config variable.
}
}