PHP code example of wunderwerkio / emitter-sdk

1. Go to this page and download the library: Download wunderwerkio/emitter-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wunderwerkio / emitter-sdk example snippets


use Wunderwerk\EmitterSDK\Emitter;

// Create Emitter instance.
$emitter = new Emitter();

// Connect to emitter server at host `localhost` and port `8080`.
// A username can be optionally passed as the third argument.
// The username is only used for the presence features (see https://emitter.io/develop/presence/).
$emitter->connect('localhost', 8080, 'my-username');

// Disconnect from server.
$emitter->disconnect();

$emitter->publish(
  key: '__channel-key-with-write-permission__',
  channel: 'article1/',
  message: 'Hello World!'
);

$emitter->subscribe(
  key: '__channel-key-with-read-permission__', 
  channel: 'article1/'
);

use Wunderwerk\EmitterSDK\EmitterInterface;

// Listen for incoming messages.
// Note: Multiple handlers can be assigned!
$handler = function (EmitterInterface $emitter, string $message, string $topic): void {
  printf('Incoming message for topic %s: %s', $topic, $message);

  // Interrupt event loop once the first message is received.
  $emitter->interrupt();
};

$emitter->addMessageHandler($handler);

// A handler can be removed again.
$emitter->removeMessageHandler($handler);

// Start event loop.
// The TRUE here means, that the event loop sleeps for a short amount of time before checking again for new messages. 
$emitter->loop(TRUE);

use Wunderwerk\EmitterSDK\EmitterInterface;

$handler = function (EmitterInstance $emitter, float $elapsedTime): void {
  printf('Loop running for %d seconds', $elapsedTime);

  // Interrupt event loop after 10 seconds runtime.
  if ($elapsedTime >= 10) {
    $emitter->interrupt();
  }
}

// Register handler.
$emitter->addLoopHandler($handler);

// Handler can be removed again.
$emitter->removeLoopHandler($handler);

$channelKey = $emitter->keygen(
  key: '__master-key-from-emitter-server__',
  channel: 'article1/',
  type: 'rwp' // Give permissions for e.g. r(ead), w(write) and p(resence)
  ttl: 0 // TTL in seconds for how long this key should be valid. 0 means key is valid indefinitely.
);


$emitter->presence(
  channelKey: '_channel-key-with-presence-permission__',
  channel: 'article1/',
  status: TRUE, // Receive full status in response.
  changes: TRUE, // Subscribe to presence changes.
)