Skip to content

wsocket-io/sdk-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wSocket PHP SDK

Official PHP SDK for wSocket — Realtime Pub/Sub over WebSockets.

Packagist License: MIT

Installation

composer require wsocket-io/sdk

Quick Start

<?php
require 'vendor/autoload.php';

$client = new \wSocket\Client('wss://node00.wsocket.online', 'your-api-key');
$client->connect();

$chat = $client->channel('chat:general');
$chat->subscribe(function ($data, $meta) {
    echo "[{$meta['channel']}] " . json_encode($data) . "\n";
});

$chat->publish(['text' => 'Hello from PHP!']);
$client->listen(); // blocking event loop

Features

  • Pub/Sub — Subscribe and publish to channels in real-time
  • Presence — Track who is online in a channel
  • History — Retrieve past messages
  • Connection Recovery — Automatic reconnection with message replay

Presence

$chat = $client->channel('chat:general');

$chat->presence()->onEnter(function ($member) {
    echo "Joined: {$member['clientId']}\n";
});

$chat->presence()->onLeave(function ($member) {
    echo "Left: {$member['clientId']}\n";
});

$chat->presence()->enter(['name' => 'Alice']);
$members = $chat->presence()->get();

History

$chat->onHistory(function ($result) {
    foreach ($result['messages'] as $msg) {
        echo "[{$msg['timestamp']}] " . json_encode($msg['data']) . "\n";
    }
});

$chat->history(['limit' => 50]);

Requirements

  • PHP >= 8.1
  • textalk/websocket >= 1.6

Development

composer install
composer test

License

MIT

About

wSocket PHP SDK — Realtime Pub/Sub client

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages