moonda/moonws

An abstract pattern for websocket-based projects, based on Ratchet.

v1.0.0 2016-06-20 15:20 UTC

This package is not auto-updated.

Last update: 2019-12-31 20:26:34 UTC


README

An abstract pattern for websocket-based projects, based on Ratchet.

This project defines an abstract pattern with high refactoring capabilities (very easy to integrate from an existing project, and very easy to migrate/refactor) in order to build lightweight and human-readable websocket servers.

This pattern aims to avoid what is known as the big spaghetti clusterfuck that happens quite often when unwarned developers start toying with websockets : centralization of code, interdependence and cycling patterns are the big oh noes that usually come with young websockets projects.

But have no fear. MoonWS is here.

Installation and requirements

PHP 5.5.0 or higher is required.

The library is available from composer. Simply type :

composer require moonda/moonws

There is no other reqirement. Usually, these types of servers fit very well in a Silex/Symfony based application, because of the "contenairization" of parameters, but it will work pretty much everywhere.

Usage

The usage is very similar to the creation of a WS Message Component from Ratchet. Read the docs.

Your main server class will change a bit :

<?php
class Chat extends WSEndpoint
{
    /**
     * The first argument is an array of controllers that should be used by the dispatcher,
     * as follow :
     *      (string) controllerName => (AbstractWSController) controller
     *
     * Requests will then be mapped using the controller name and method name in the
     * "action" field of the message, e.g :
     * {
     *      "action" => "player/playTurn"
     * }
     * will call the public method "playTurn" from the controller
     * attached to the key "player".
     */
    public function __construct()
    {
        parent::__construct(array(
            "chat" => new ChatController()
        ));
    }
}

Of course, you can override the base methods, such as onOpen, onError, etc... However, it is recommended NOT to override the onMessage method, since it is the starting point of the whole pattern.

Your controller class would look like this then :

<?php
class ChatController extends AbstractWSController
{
    public function sayHelloToMyFriends(ConnectionInterface $conn, $msg){
        /**
         * @var $client ConnectionInterface
         */
        foreach ($this->endpoint->getCollections()->getClients() as $client){
            $this->sendMessageTo($client, array(
                "me" => $conn->{'resourceId'},
                "my_message" => "Hello Friend !",
                "what_i_sent" => $msg
            ));
        }
    }
}

Happily start your fresh new server :

<?php
$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    9999
);

And the method sayHelloToFriends can then be called from a client :

var ws = new WebSocket("ws://127.0.0.1:9999");
ws.send(JSON.stringify({
    "action": "chat/sayHelloToMyFriends",
    "yeaha": "yeah !"
}));