PHP code example of oshitsd / php-socket

1. Go to this page and download the library: Download oshitsd/php-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.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oshitsd / php-socket example snippets


use Oshitsd\PhpSocket\Facades\PhpSocket;

PhpSocket::connect();

use Oshitsd\PhpSocket\Facades\PhpSocket;

$connect = PhpSocket::connect([
    "room" => "DEMO_CHAT_ROOM" // Required: Name of the chat room to join
    "role" => "user", // Optional: User role, defaults to 'user'. Options: 'user' or 'agent'
    "userId" => 8001, // Optional: Unique user identifier
    "userName" => "OSHIT SUTRA DAR", // Optional: Display name of the user
]);

$response = PhpSocket::send([
    "event" => "demo_chat", // Required: Name of the event to broadcast
    "to" => "all", // Target recipient(s). Options: 'all' (broadcast to everyone) or a specific user ID to send a private message.
    "message" => [
        "time" => date('Y-m-d H:i:s'),
        "text" => "Laravel says hi 👋",
        "user" => [
            "id" => 8001,
            "name" => "OSHIT SUTRA DAR"
        ]
    ]
]);

$response = PhpSocket::receiveAck();

$close = PhpSocket::close();

use Illuminate\Support\Facades\Route;
use Oshitsd\PhpSocket\Facades\PhpSocket;

Route::get('send-notification', function () {

    $connect = PhpSocket::connect([
        "room" => "DEMO_CHAT_ROOM",
        "userId" => 8001,
        "userName" => "OSHIT SUTRA DAR",
    ]);

    $response = PhpSocket::send([
        "event" => "demo_chat",
        "to" => "all",
        "message" => [
            "time" => date('Y-m-d H:i:s'),
            "text" => "Laravel says hi 👋",
            "user" => [
                "id" => 8001,
                "name" => "OSHIT SUTRA DAR"
            ]
        ]
    ]);

    $close = PhpSocket::close();

    // Return the connection and message response
    return response()->json([
        'connect' => $connect,
        'response' => $response,
        'close' => $close
    ]);
});
bash
composer 
bash
php artisan vendor:publish --tag=phpsocket-config
env
PHP_SOCKET_ENV=production
PHP_SOCKET_HOST=socket.techcanvas.info
PHP_SOCKET_API_KEY=demo-tech-canvas-api-key