kostandinvllahu/socket-backend

A Laravel package for backend personalised Socket.IO integration

v1.0.1 2025-07-16 08:11 UTC

This package is auto-updated.

Last update: 2025-07-16 08:15:48 UTC


README

A Laravel package for backend personalized Socket.IO integration.

Features

  • Easily integrate backend Socket.IO event triggering in your Laravel applications
  • Configurable via environment variables and config file
  • Simple and reusable API for sending socket events

Installation

  1. Require the package via Composer:
composer require kostandinvllahu/socket-backend
  1. Publish the configuration file:
php artisan vendor:publish --tag=config
  1. Add the necessary environment variables to your .env file:
SOCKET_URL='http://your-socket-server-url/socket_namespace'
SOCKET_URL_TRIGGER_EVENT='http://your-socket-server-url/trigger'
SOCKET_NAMESPACE='your-socket-namespace'
SOCKET_SECRET_KEY='your-secret-key'

Usage

Call the static method anywhere in your Laravel app to trigger socket events:

use Kostandinvllahu\SocketBackend\SocketTrigger;

SocketTrigger::customisedSocket('your.event.name', ['key' => 'value']);

Configuration

The package configuration file config/socket.php contains:

return [
    'url' => env('SOCKET_URL_TRIGGER_EVENT', ''),
    'namespace' => env('SOCKET_NAMESPACE', ''),
    'SECRET_KEY' => env('SOCKET_SECRET_KEY', ''),
];

You can customize these values in your .env file or override them directly in the config file after publishing.

Declaration in blade


<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>

<script>
    const socketUrl = "{{ env('SOCKET_URL') }}";
    const secretKey = "{{ env('SOCKET_SECRET_KEY') }}"; // This will be an empty string if not set

    const options = {
        transports: ['websocket'],
    };

    // Add auth only if secretKey is present
    if (secretKey) {
        options.auth = {
            secret_key: secretKey
        };
    }

    const socket = io(socketUrl, options);

    // Listen for the event sent from Laravel
    socket.on('eventName', function (data) {
        alert('Received from server: ' + JSON.stringify(data));
    });
</script>

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues or submit a merge request.

Support

For questions or support, open an issue on the GitLab repository or contact the maintainer directly.

License

This project is open-sourced software licensed under the MIT license.

Authors