oshitsd/php-socket

A PHP package to interact with WebSocket servers. It provides methods to connect, send, receive, and close WebSocket connections, with Laravel integration for seamless usage.

v1.0.4 2025-06-16 09:20 UTC

This package is auto-updated.

Last update: 2025-06-18 06:56:39 UTC


README

PhpSocket is a Laravel wrapper for handling Socket.IO WebSocket communication using a simple, expressive API. Built on top of the textalk/websocket client, it allows your Laravel app to communicate easily with a Socket.IO server.

๐Ÿš€ Features

  • Connect to a Socket.IO WebSocket server
  • Send and receive messages
  • Built-in support for custom payloads and events
  • Laravel Facade for simple usage
  • Configurable host and port

๐Ÿงฐ Requirements

  • PHP 7.4 or higher
  • Laravel 8, 9, 10, 11, 12 or upper
  • WebSocket server with Socket.IO (EIO=4) support

๐Ÿ“ฆ Installation

Install via Composer:

composer require oshitsd/php-socket

Publish the config file:

php artisan vendor:publish --tag=phpsocket-config

This will create a config/phpsocket.php file where you can configure the WebSocket host and port.

โš™๏ธ Configuration

In your .env file, add:

PHP_SOCKET_ENV=production
PHP_SOCKET_HOST=socket.techcanvas.info
PHP_SOCKET_API_KEY=demo-tech-canvas-api-key

Or modify the config/phpsocket.php file directly.

๐Ÿงช Basic Usage

๐Ÿ”Œ Connect to Socket

Without Authentication

use Oshitsd\PhpSocket\Facades\PhpSocket;

PhpSocket::connect();

With Authentication

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
]);

๐Ÿ“ค Send a Message

$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"
        ]
    ]
]);

๐Ÿ“ฉ Receive Message Response

$response = PhpSocket::receiveAck();

๐Ÿ”’ Close the Connection

$close = PhpSocket::close();

๐Ÿ›ฃ๏ธ Example Route Usage

You can quickly test sending a message using a simple route in your Laravel application.

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
    ]);
});

When you visit http://your-app-url/send-notification, this route will:

  • Connect to the WebSocket server
  • Send a message to all connected clients
  • Return the connection and response details as a JSON response

๐Ÿ” Live Demo

Once you hit the route http://your-app-url/send-notification, the message will be broadcasted to all connected clients.

You can view the real-time message output here: ๐Ÿ‘‰ Chat App Demo

๐Ÿงผ Example Output

๐Ÿ‘‹ Connected to socket server successfully.
๐Ÿ“ค Message sent.
๐Ÿ“จ Received: {message response}
๐Ÿ”’ Connection closed.

๐Ÿงช Testing

You can run the package tests with:

./vendor/bin/phpunit

Tests are located in the tests/ directory.

๐Ÿ“„ License

This package is open-source software licensed under the MIT license.

๐Ÿ‘จโ€๐Ÿ’ป Credits

Developed by OSHIT SD
WebSocket client powered by textalk/websocket

Packagist Version