Skip to content

SocketBus/socketbus-php

Repository files navigation

SocketBus PHP Library

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages