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.0 2025-04-24 11:25 UTC

This package is auto-updated.

Last update: 2025-04-24 11:35:21 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 8.0 or higher
  • Laravel 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=config

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

โš™๏ธ Configuration

In your .env file, add:

SOCKET_HOST=127.0.0.1
SOCKET_PORT=3000

Or modify the config/php-socket.php file directly.

๐Ÿงช Basic Usage

Connect to Socket

use PhpSocket;

PhpSocket::connect();

Send a Message

PhpSocket::send([
    "event" => "LARA_NOTIFY",
    "to" => "all",
    "message" => [
        "time" => date('Y-m-d H:i:s'),
        "text" => "Laravel says hi ๐Ÿ‘‹",
        "user" => [
            "id" => 1,
            "name" => "OSHIT SUTRA DAR"
        ]
    ]
]);

Receive a Message

$response = PhpSocket::receive();

Close the Connection

PhpSocket::close();

๐Ÿงผ Example Output

๐Ÿ‘‹ Connected to socket server successfully.
๐Ÿ“ค Message sent.
๐Ÿ“จ Received: {message}
๐Ÿ”’ 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 Oshitsd
WebSocket client powered by textalk/websocket