Sandstone

Build a real time RestApi.

Sandstone is a PHP microframework designed to build a RestApi working together with a websocket server.

What can I do with Sandstone

Microframework means you can declare RestApi endpoints and websocket topics in a minimalist way.

Working together means you can send push notifications to web client even from RestApi controllers.

So you can mount real time RestApi, then:

Live examples

Create a simple chat topic

$app->topic('chat/demo', function ($topicPattern) {
    return new class ($topicPattern) extends Topic
    {
        public function onPublish(WampConnection $conn, $topic, $event)
        {
            $this->broadcast([
                'message' => $event,
            ]);
        }
    };
});
session.publish('chat/demo', '
');

Dispatch push event on Rest API call

It is possible to dispatch an event from Rest Api controller, and listen to it from a websocket topic for i.e broadcast it.

See that live example of a simple Push event:

RestApi stack

/**
 * Dispatch push event on Rest API call
 */
$app->post('api/hello/{name}', function ($name) use ($app) {
    $event = new HelloEvent();
    $event->name = $name;

    $app['dispatcher']->dispatch('event.hello', $event);

    return new JsonResponse(['hello' => $name]);
})->value('name', 'world');

// Forward event to websocket process
$app->forwardEventToPushServer('event.hello');

Websocket stack

/**
 * Topic that broadcast push event
 */
$app->topic('push/demo', function ($topicPattern) {
    return new class ($topicPattern) extends Topic implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [
                'event.hello' => 'onHelloEvent',
            ];
        }

        public function onHelloEvent(HelloEvent $event)
        {
            $this->broadcast([
                'message' => "Someone called Rest Api /api/hello. Hello $event->name !",
            ]);
        }
    };
});

How to get started

Using microframework

If you're familiar with microframeworks (i.e expressJS, Python Flask, or PHP Silex), You can build a Sandstone application with only a few PHP files. See this full example.

This way, you can create a fully customizable application.

Using Sandstone edition

To create an application with already installed a database, Symfony web profiler... in a Docker environment, You may be insterested in the edition.

This way, you can quickly bootstrap a Sandstone application, then be guided by the cookbook to add endpoints, topics...

Working examples

Full example

Chat example

Authenticated application

Chat demo of this page

Push demo of this page