GithubHelp home page GithubHelp logo

vinelab / minion Goto Github PK

View Code? Open in Web Editor NEW
126.0 13.0 16.0 107 KB

A Simplified Client for WAMP v2 (Web Application Messaging Protocol) with command line support - PHP WebSocket Made Easy

License: MIT License

PHP 100.00%

minion's Introduction

Build Status

SensioLabsInsight

Minion

A simplified client the WAMP v2 protocol (Web Application Messaging Protocol) with a handy command line tool - PHP WebSocket made easy.

Based on the great work put together by Thruway, Minion will give you the simplicity and flexibility of running minion run and get a client running in no time. In addition to helping you structure your application. See How It Works for details.

For a jump-start head over to the Quick Start Guide or read on for detailed docs. Or you may take a look at the Examples to get an idea about how this works.

Installation

Composer

Add the following to require in composer.json

"vinelab/minion": "*"

Then run composer update to install.

Laravel Bounties

  • Add Vinelab\Minion\MinionServiceProvider to the providers array in your app.php and you'll have a Minion facade available throught your project.
  • The command line tool is available through artisan as php artisan minion:run see CLI
  • Run php artisan vendor:publish and head to app/config/minion.php to configure your minion.

Configuration

Configure the connection parameters you want your client to use when it connects to a WAMP router.

Router

$m = new Minion():
$m->run(['realm' => 'myrealm', 'host' => 'some.host.ws', 'port' => 8182]);

Provider Registration

$m = new Minion();
$m->register('ChatProvider');
$m->register('MyApp\Providers\NotificationProvider'):
$m->run();

You may also find it useful to list the providers in the config as such:

$m = new Minion();
$m->run(
    [
        'port'     => 9876,

        'host'     => 'the.host',

        'realm'    => 'somerealm',

        'register' => [
            'ChatProvider',
            'SomeOtherProvider'
            'NotificationProvider',
        ]
    ]
);

In existing applications it may be useful to be re-use an existing ReactPHP loop. You can pass in a LoopInterface like so:

$loop = React\EventLoop\Factory::create();
$m = new Minion();
$m->run([], $loop);

Usage

The idea behind Minion is to help structure your application and get it ready for scale with real-time communication by using providers to register RPCs and publish and subscribe to topics with predefined functionalities to make things quick. For more about RPCs and Pub/Sub see Introduction to WAMP programming

How It Works

WAMP is a protocol that defines a Router that handles connections of clients, your application is one of these clients and the application logic is implemented within providers which you can register with Minion using the register($provider) method. A provider can be the name of a class (full namespace if applicable) or a Closure.

Consider the following directory structure:

src/
vendor/
start.php
composer.json

Provider Classes

  • Provider classes is where your application logic resides, Minion uses topic prefixes as a convention to distinguish providers and that is done by specifying a protected $prefix = 'topic.prefix.'; in your provider class.

It is a convention to use dot '.' separated prefixes such as chat. which will result in topic read end up being chat.read

  • Every provider class must extend Vinelab\Minion\Provider and implement public function boot() method which is the best place to have your registrations and pub/sub operations.

  • Every method registered or subscribed will receive the $args and $data when involved. Consider this method

    public function get($args, $data)
    • $args is the array of the args passed from the call
    • $data is a Dictionary instance where you can safely access attributes like $data->something and when they don't exist you get a null value instead of an error as in StdClass objects, though you may use the $data variable as you would use any other object with isset($data->prop) and empty($data->prop)
  • src/ChatProvider.php

<?php

use Vinelab\Minion\Provider;

class ChatProvider extends Provider
{
    protected $prefix = 'chat.';

    public function boot()
    {
        // will be registered to topic: chat.send
        $this->register('send', 'sendMessage');
    }

    public function sendMessage($args, $data)
    {
        $message = $data->message;

        // store message in the database

        // tell everyone about it
        $this->publish('message', compact('message'));

        // response with the status
        return true;
    }
}
  • start.php
use Vinelab\Minion\Minion;

$m = new Minion;
$m->register('ChatProvider');
$m->run();

Closures as Providers

  • start.php
require __DIR__.'/vendor/autoload.php'

use Vinelab\Minion\Minion;
use Vinelab\Minion\Client;

// Get a minion instance
$m = new Minion;

$add = function ($x, $y) { return $x + $y; };

// Register a closure provider
$m->register(function (Client $client) use ($add) {

    // register
    $client->register('add', $add);

    // subscribe
    $client->subscribe('some.topic', function ($data) {
        // do things with data
        $data->key;
        $data->other_key;
    });

    // publish
    $client->publish('i.am.here', ['name' => 'mr.minion']);
});

CLI

Minion comes with a handy command line tool for usage straight from the command line. Once you install using composer a minion binary will be in your vendor/bin/. To make things easier you can run export PATH="./vendor/bin:$PATH" to use minion run straight instead of ./vendor/bin/minion run

use minion list for a list of available commands and minion --help [command] for more info about each of them.

Commands
  • run
    • Options
      • --realm: Specify WAMP realm to be used
      • --host: Specify the router host
      • --port: Specify the router port
      • --register: Register provider classes (can be used multiple times)
    • Example minion run --realm=chatting --port=9876 --register="ChatProvider" --register="MyApp\Providers\NotificationsProvider"

Crossbar.io

Minion ships with a minimal crossbar.io config file which you can find at ./vendor/vinelab/minion/.crossbar/config.json and to start crossbar using it run crossbar start --cbdir ./vendor/vinelab/minion/.crossbar

To get started with Crossbar visit the Quick Start with Crossbar.io Guide.

For more information about crossbar head over to Crossbar.io Quick Start.

Contributing

Pull Requests are most welcome! Dev packages are specified in composer.json under require-dev

  • Run tests with ./vendor/bin/phpunit
  • Coding standards must be checked before committing code by issuing: ./vendor/bin/phpcs --standard=phpcs.xml src
  • In the case of violations use ./vendor/bin/php-cs-fixer fix src which will help solve them out

License

Minion is distributed under the MIT License, see the LICENSE file in this package.

minion's People

Contributors

lajosbencz avatar miholeus avatar mulkave avatar nek- avatar nelind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minion's Issues

Connection issue with autobahn.js

Dears,

I have followed the instructure in the Read Me to make it work with laravel but it's not working

I got these errors in the command line and it's repeated

2014-12-06T16:51:31.7175210 info [Thruway\Transport\PawlTransportProvider 11798] Starting Transport
2014-12-06T16:51:31.7187670 info [Thruway\Transport\PawlTransportProvider 11798] Could not connect: Connection refused

I also tried to open a connection using autobahn.js but it gives me this issues in the browser console

WebSocket connection to 'ws://127.0.0.1:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

Publishing from backend

Is there anyway to simply publish from a backend?
front to front end is easy.. but since minion uses providers..
Not able to used in controller or model.

Any suggestions?

Edit: Also the config file is not used ?

laravel 5.4 call to member function call() on null

Fatal error: Uncaught Error: Call to a member function call() on null in C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\laravel\framework\src\Illuminate\Console\Command.php:182
Stack trace:
#0 C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\symfony\console\Command\Command.php(264): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#1 C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\laravel\framework\src\Illuminate\Console\Command.php(167): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#2 C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\symfony\console\Application.php(874): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\symfony\console\Application.php(22 in C:\Users\Caramelo-Rik\ownCloud\Projects\local-dev\x\vendor\laravel\framework\src\Illuminate\Console\Command.php on line 182

How to publish from a controller

I know this isn't a bug, but I've worked it all out except how to publish from a controller, not the Service Provider. There may be a gap in my understanding but how do I do it? For example when a user's details are updated, I'd like to send a broadcast to anyone watching that.

Thanks

Authentication

Hi,

Would like to know how to implement authentication using Minion, if there are examples available please link me.

Push support

Is there any way to push a message to the client from another script? If there is, could you update the readme with an example? Thanks!

Update Thruway dependency to 0.3

Hi there,

The Thruway package is in another version than the one minion requires.
Could you update and test and see if the new version is compatible?

"voryx/thruway": "0.3.*"

Getting Invalid Provider Exception

I use this plugin to get notification in my client app ( angularjs ) from server ( Laravel 4.2 ), I implement autobahn.js at client side, but when i implement Vinelab/minion library in Laravel and trying to run minion by calling php artisan minion:run it give me error like

 [Vinelab\Minion\InvalidProviderException]
 Provider Vinelab\Minion\ChatProvider must be an instance of \Vinelab\Minion
 \Provider

directory structure

  • app/
    • config/
      • packages/
        • vinelab/
          • minion/
            • minion.php
    • providers/
      • ChatProvider.php
  • vendor/

and in app/config/packages/vinelab/minion/minion.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Router Realm
    |--------------------------------------------------------------------------
    |
    | The realm that the router should use.
    |
    */
    'realm' => 'minion',

    /*
    |--------------------------------------------------------------------------
    | Router Host
    |--------------------------------------------------------------------------
    |
    | The IP or hostname that the router should run under.
    |
    */
    'host' => '127.0.0.1',

    /*
    |--------------------------------------------------------------------------
    | Router Port
    |--------------------------------------------------------------------------
    |
    | The port that should be used by the router.
    |
    */
    'port' => 9090,

    /*
    |--------------------------------------------------------------------------
    | Auto-registered Providers
    |--------------------------------------------------------------------------
    |
    | The providers listed here will be automatically registered on the
    | session start of the router, in return their role is to register RPCs,
    | subscribe and publish to topics and pretty much whatever an Internal Client does.
    |
    */
    'providers' => [

        'ChatProvider'

    ],

    'debug' => true,

];

and in providers/ChatProvider.php

<?php

use Vinelab\Minion\Provider;

class ChatProvider extends Provider {

    protected $prefix = 'order.';

    public function boot()
    {
        // will be registered to topic: chat.send
        $this->register('send', 'sendMessage');
    }

    public function sendMessage($args, $data)
    {
        $message = $data->message;

        // store message in the database

        // tell everyone about it
        $this->publish('message', compact('message'));

        // response with the status
        return true;
    }
    }
}

I check for various name space and lot of things but i can not solve this.

Please tell where i am wrong.

Thanks...

i can't understand your document!

hi
i am building an real time app using Laravel and Wamp2
i used voryx/Thruway for Wamp2 router and wampy.js for browser and your lib for php client
and i actually don't understand your documents
can you be more clear in document? and write about codes should be in which file and what it's the way i should start using this lib.
or better give us one simple Laravel app code to see and explore.

thanks.

Crossbar.io

Is crossbar needed? To get up and running?
What about things like Zer0mq?

I've got used to the Ratchet way of doing things, like in their tutorials. Too bad that's Wamp v1 and isn't compatible with angular router.

Invalid Provider Exception

Hello!
First of all.. I'm are a composer noob, so I'm not much familiarized with him.

I are trying to test the "ChatProvider" example in front page, with no luck...

I do a folder "minion" and inside put a composer.json file with "vinelab/minion": "*" then I ran "composer update" and everything installs.
I create "start.php" and "ChatProvider.php" as tutorial says and I put in your place:

├── composer.json
├── composer.lock
├── composer.phar
├── src
│   └── ChatProvider.php
├── start.php
└── vendor
    ├── autoload.php
    ├── bin
    ├── cboden
    ├── composer
    ├── doctrine
    ├── evenement
    ├── guzzle
    ├── illuminate
    ├── leo
    ├── nesbot
    ├── paragonie
    ├── psr
    ├── ratchet
    ├── react
    ├── symfony
    ├── vinelab
    └── voryx

I tried start with php -f start.php and I get a error:

PHP Fatal error: Uncaught exception 'Vinelab\Minion\InvalidProviderException' with message 'Provider ChatProvider must be an instance of \Vinelab\Minion\Provider' in /var/www/minion/vendor/vinelab/minion/src/Vinelab/Minion/Minion.php:43

What may be missing?
I'm not are using Laravel or other stuff...
Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.