Sandstone is a PHP microframework designed to build a RestApi working together with a websocket server.
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:
$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', '
');
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 !",
]);
}
};
});
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.
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...