sciactive/nymph-pubsub

A library for publishing/subscribing to Nymph database changes.

3.0.0-beta.2 2017-11-17 00:00 UTC

README

Latest Stable Version License Open Issues

Nymph is an object data store that is easy to use in JavaScript and PHP.

Installation

Automatic Setup

The fastest way to start building a Nymph app is with the Nymph App Template.

Nymph App Template

Manual Installation

You can install Nymph PubSub Server with Composer.

composer require sciactive/nymph-pubsub

This repository is the PHP publish/subscribe server. For more information, you can see the main Nymph repository.

Setting up a Nymph PubSub server

Quick Setup with Composer
composer require sciactive/nymph-pubsub
// pubsub.php: Start with `php pubsub.php [-d]`

if (php_sapi_name() != "cli") {
  die("You can only run pubsub.php from the command line.");
}

// This is an example server that is configured with hostname
// "pubsubnetwork1entry" as an entry point to network1, which contains two
// endpoint servers, "pubsubnetwork1endpoint1" and "pubsubnetwork1endpoint2".

// Setting a default timezome is highly recommended.
date_default_timezone_set('America/Los_Angeles');

require 'vendor/autoload.php';

// Set up Nymph.
use Nymph\Nymph;
Nymph::configure([
  'MySQL' => [
    'host' => 'your_db_host',
    'database' => 'your_database',
    'user' => 'your_user',
    'password' => 'your_password'
  ]
]);

\Nymph\Nymph::connect();

// Load all the entities that will be accessible in this server.
require 'MyEntityA.php';
require 'MyEntityB.php';

// Allow this file to be called with "-d" to daemonize it.
if (in_array('-d', $argv)) {
  function shutdown() {
    posix_kill(posix_getpid(), SIGHUP);
  }

  // Switch over to daemon mode.
  if ($pid = pcntl_fork()) {
    return;
  }

  register_shutdown_function('shutdown');
} else {
  error_reporting(E_ALL);
}

// Set up Nymph PubSub.
$config = include(__DIR__.'/config.php');
$config['port'] = 8080;
$config['relays'] = [
  'ws://pubsubnetwork1endpoint1:8080/',
  'ws://pubsubnetwork1endpoint2:8080/'
];
$server = new \Nymph\PubSub\Server($config);

// Run the server.
$server->run();
// config.php

// This config file tells Nymph to publish entity updates to these network entry
// points. They will then relay the publish to their network.

return [
  'entries' => [
    'ws://pubsubnetwork1entry:8080/',
    'ws://pubsubnetwork2entry:8080/',
    'ws://pubsubnetwork3entry:8080/'
  ]
];
// somewhere in your Nymph rest endpoint.
$config = include('path/to/pubsub/config.php');
\Nymph\PubSub\Server::configure($config);

For a thorough step by step guide to setting up Nymph on your own server, visit the Setup Guide.

API Docs

Check out the API Docs in the wiki.