socketbus/socketbus-php

There is no license information available for the latest version (v1.0.1) of this package.

v1.0.1 2021-01-06 21:05 UTC

This package is auto-updated.

Last update: 2024-09-07 05:08:42 UTC


README

The SocketBus PHP Library

Getting Started

  1. Create an account -
  2. Minimum requirements -
  3. Install the library -
    composer require socketbus/socketbus-php
  4. Using the library -

Create a SocketBus instance

require 'vendor/autoload.php';

use SocketBus\SocketBus;

$config = [
    'app_id' => 's-1-J2PCu8g8sAejZeXx',
    'secret' => 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj'
];

$socketBus = new SocketBus($config);
End-to-End Encryption

To ensure that your data is secure, you can enable End-to-end encryption under Application > Settings. This setting in combination with the option custom_encryption_key encrypts the payload and decrypts in the client when an authenticated user receives a encrypted payload.

$config = [
    'app_id' => 's-1-J2PCu8g8sAejZeXx',
    'secret' => 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj',
    'custom_encryption_key' => 'my-unique-key'
];

Authentication

Defining the channel type
$channelName = $request->channel_name;

if ($socketBus->isChannelPrivate($channelName)) {
    // The channel is private
}
if ($socketBus->isChannelPresence($channelName)) {
    // The channel is presence
}
if ($socketBus->isChannelPublic($channelName)) {
    // The channel is public
}
Private Authentication
$socketId = $request->socket_id;
$channelName = $request->channel_name;

if (/** verifies if user can access the request channel */) {
    // returns the token to the client
    return [
        'auth' => $socketbus->auth($socketId, $channelName)
    ];
}
Presence Authentication
$socketId = $request->socket_id;
$channelName = $request->channel_name;
$userId = /** gets the current user id */;

if (/** verifies if user can access the request channel */) {
    // returns the data
    return $socketbus->authPresence($socketId, $channelName, $userId, true);
}

Broadcasting

$payload = [
    'type' => 'cupcake',
    'flavor' => 'sweet'
];

$sockebus->broadcast('food-observer', 'new-food', $payload);

Resources

API Docs - Check-out the full documentation

Related Projects

SocketBus Laravel Library - Laravel library