2017 © Pedro Peláez
 

library sandstone

Silex RestApi with Websockets.

image

eole/sandstone

Silex RestApi with Websockets.

  • Friday, May 25, 2018
  • by alcalyn
  • Repository
  • 9 Watchers
  • 61 Stars
  • 2,800 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 29 Versions
  • 31 % Grown

The README.md

Sandstone

Build Status Latest Stable Version Scrutinizer Code Quality SensioLabsInsight License, (*1)

PHP microframework designed to build a RestApi working together with a websocket server., (*2)

Build a real time RestApi!, (*3)

:speech_balloon: New (18 April 2018)

I opened a chat channel where you can get help, give feedback, and talk about Sandstone (Mattermost instance):, (*4)

:speech_balloon: https://framateam.org/sandstone :speech_balloon:, (*5)

Install

``` bash composer require eole/sandstone, (*6)



## Usage ### Create a Sandstone application Sandstone is a Silex application with websockets: ``` php $app = new Eole\Sandstone\Application();

Declare a websocket topic

Just as easy as declaring a silex route:, (*7)

``` php $app->topic('chat/{channel}', function ($topicPattern, $arguments) { $channelName = $arguments['channel'];, (*8)

return new ChatTopic($topicPattern, $channelName);

});, (*9)


See [ChatTopic class here](https://eole-io.github.io/sandstone-doc/examples/multichannel-chat). ### Send push notifications When an endpoint is called on the RestApi, i.e `POST /api/articles` and update a resource, you can send a push notification to notify this update. On the RestApi stack: ``` php use Symfony\Component\HttpFoundation\Response; $app->post('api/articles', function () use ($app) { // Dispatch an event on article creation $app['dispatcher']->dispatch('article.created', new ArticleEvent()); return new Response([], 201); }); // Send all 'article.created' events to push server $app->forwardEventToPushServer('article.created');

Then on the websocket stack:, (*10)

``` php use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Eole\Sandstone\Websocket\Topic;, (*11)

class MyWebsocketTopic extends Topic implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ 'article.created' => 'onArticleCreated', ]; }, (*12)

public function onArticleCreated(ArticleEvent $event)
{
    $this->broadcast([
        'message' => 'An article has just been published: '.$event->title,
    ]);
}

} ```, (*13)

Examples

Working examples from scratch:, (*14)

Documentation

See the full documentation here, (*15)

Sandstone documentation, (*16)

Sandstone edition

You're planning to start a new real-time Rest Api application based on Sandstone?, (*17)

You may be interested by Sandstone edition., (*18)

It already integrates a Sandstone application with a docker environment, a database, debug tools..., (*19)

Get started with Sandstone edition., (*20)

Misc

Articles about Sandstone:, (*21)

Big picture: https://eole-io.github.io/sandstone-doc/big-picture, (*22)

Changelog

See Releases page., (*23)

License

This library is under MIT License., (*24)

The Versions