Developed with love by KnpLabs Hire us for your project!
38

Hoa-WebSocket-Bundle

by atipik

This bundle allows you to use Hoa/Websocket/Server and Hoa/Websocket/Client into Symfony2.

Atipik/HoaWebSocketBundle

Build Status

This bundle allows you to use Hoa/Websocket/Server and Hoa/Websocket/Client into Symfony2.

With a simple configuration, you will be able to launch your WebSocket server

1. Installation

1.1 Update your composer.json

Add these lines to your require section:

{
    "require": {
        "atipik/hoa-websocket-bundle" : "1.*@dev"
    }
}

1.2 Install dependencies

composer update

1.3 Update your AppKernel.php file

<?php
# app/AppKernel.php
// ...

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Atipik\Hoa\WebSocketBundle\AtipikHoaWebSocketBundle(),
            // ...
        );

        return $bundles;
    }

    // ...
}

1.4 Configure your project

To enable this bundle, you must add these lines in your config.yml:

# app/config/config.yml
atipik_hoa_web_socket: ~

You can also specify address and port.

# app/config/config.yml
atipik_hoa_web_socket:
    address : 1.3.3.7
    port    : 4242

By default, server will be started on 127.0.0.1:8080

2. Architecture

The Symfony 2 command php app/console hoa:websocketserver has only one job: calling a runner.

The runner links modules (your logic code) and WebSocket events which are managed by Atipik\Hoa\WebSocketBundle\WebSocket\Server.

When the WebSocket server receives an event, the runner catches it and calls all subscribed modules.

3. Server

3.1 Simple Usage

  • In your bundle, create a class which extends Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module:
<?php
# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
}
  • Update your services.yml:
# src/My/Bundle/Resources/config/services.yml
services:
    my_bundle.my_module:
        class: My\Bundle\WebSocket\Module\MyModule
        tags:
            - { name: atipik_hoa_web_socket.module }

Don't forget to add atipik_hoa_web_socket.module tag !

  • In this class, implement the getSubscribedEvents method:
<?php
# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    public function getSubscribedEvents()
    {
        return array(
            'open'    => 'onOpen',
            'message' => 'onMessage',
        );
    }
}
  • Add logic code:

You can get the current bucket by using $this->getBucket().

<?php
# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    public function getSubscribedEvents()
    {
        return array(
            'open'    => 'onOpen',
            'message' => 'onMessage',
        );
    }

    public function onOpen()
    {
        $this->getLogger()->log('Here comes a new challenger !');
    }

    public function onMessage()
    {
        $data = $this->getBucket()->getData();

        $this->getLogger()->log(
            'Data received in %s: %s',
            __METHOD__,
            $data['message']
        );
    }
}
  • Launch your WebSocket server:
php app/console hoa:websocketserver

3.2 Advanced Usage

3.2.1 Modules group

If you want to scale your server, you can affect modules to different group and launch server for one or more group.

You can affect many modules in the same group

If you launch the server without group, all modules will be used.

  • Just override getGroup() method in your module:
<?php
# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    // ...

    /**
     * Returns group name
     *
     * @return string
     */
    public function getGroup()
    {
        return 'foo';
    }

    // ...
}

  • Launch your server by specifying group:
php app/console hoa:websocketserver -g foo

You can also specify more than one group:

php app/console hoa:websocketserver -g foo -g bar

3.2.2 Override runner class

If you want to modify how the runner works, you should override its class:

  • Create a runner class that extends Atipik\Hoa\WebSocketBundle\WebSocket\Runner.
  • Update your services.yml:
# src/My/Bundle/Resources/config/services.yml
parameters:
    atipik_hoa_web_socket.runner.class: My\Bundle\WebSocket\Runner

3.2.3 Override server class

If you want to modify how the WebSocket Server works, you should override its class:

  • Create a server class which extends Atipik\Hoa\WebSocketBundle\WebSocket\Server.
  • Update your services.yml:
# src/My/Bundle/Resources/config/services.yml
parameters:
    atipik_hoa_web_socket.server.class: My\Bundle\WebSocket\Server

3.2.4 Override node class

Hoa/Websocket allows you to override node class to add your own data.

Of course, this bundle allow you to specify which class to use:

  • Create a node class which extends Hoa\Websocket\Node:
<?php
# src/My/Bundle/WebSocket/Node.php
namespace My\Bundle\WebSocket;

class Node extends \Hoa\Websocket\Node
{
    protected $myData;

    public function getMyData()
    {
        return $this->myData;
    }

    public function setMyData($data)
    {
        $this->myData = $data;

        return $this;
    }

    public function doThingsWithMyData()
    {
        // ...
    }
}
  • Update your services.yml:
# src/My/Bundle/Resources/config/services.yml
parameters:
    atipik_hoa_web_socket.node.class: My\Bundle\WebSocket\Node
  • You can now access the current node, an instance of your node class:
<?php
# src/My/Bundle/WebSocket/Module/MyModule.php
namespace My\Bundle\WebSocket\Module;

use Atipik\Hoa\WebSocketBundle\WebSocket\Module\Module;

class MyModule extends Module
{
    // ...

    public function onEvent1()
    {
        // ...

        $node = $this->getNode();

        $node->setMyData('foobar');

        // ..
    }

    // ...

    public function onEvent2()
    {
        // ...

        $node = $this->getNode();

        $node->getMyData(); // contain 'foobar' set in event1

        // ..
    }

    // ...
}

4. Client

If you want to communicate with a WebSocket Server, you can use service atipik_hoa_web_socket.client by using $this->get('atipik_hoa_web_socket.client') in your controller or to inject this service directly in services.yml.

For more documentation about WebSocket Client, see Hoa/WebSocket's documentation.

5. Launch unit tests

composer update
./vendor/bin/atoum

6. More documentation

See Hoa/WebSocket's documentation) to know how to use How/WebSocket and to have an example of JavaScript code.

The MIT License (MIT)

Copyright (c) 2013 Atipik

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
atipik_hoa_web_socket:
listen_address: ~
listen_port: ~
address: ~
port: ~
  • Merge pull request #9 from InTimesIT/master
    By web-flow, 2 years ago
  • The reserved indicator "@" cannot start a plain scalar
    By , 2 years ago
  • Fix exception path
    By marmotz, 2 years ago
  • Merge branch 'master' of github.com:Atipik/Hoa-WebSocket-Bundle
    By marmotz, 2 years ago
  • Throw an exception in error
    By marmotz, 2 years ago
  • Merge branch 'master' of github.com:Atipik/Hoa-WebSocket-Bundle
    By marmotz, 2 years ago
  • Update composer.json
    By marmotz, 2 years ago
  • Drop php 5.4 support
    By marmotz, 3 years ago
  • Convert Hoa\Core\Event\Bucket to Hoa\Event\Bucket
    By marmotz, 3 years ago
  • Update to hoa\Websocket 3.*
    By marmotz, 3 years ago
  • Rollback "clear bucket" feature
    By marmotz, 4 years ago
  • Clear bucket after module call
    By marmotz, 4 years ago
  • Fix configuration
    By marmotz, 4 years ago
  • Add separate adresses/ports for server and client
    By marmotz, 4 years ago
  • Merge branch 'master' of github.com:Atipik/Hoa-WebSocket-Bundle
    By marmotz, 4 years ago
  • hide error
    By marmotz, 4 years ago
  • Merge pull request #6 from stephpy/hoa_tagged_version
    By marmotz, 4 years ago
  • Bundle uses a stable version of hoa\websocket \o/. User'll no more have to define each hoa dependencies in dev.
    By stephpy, 4 years ago
  • Move "hoa" conf to "atipik_hoa_web_socket". Default parameters are now in services.yml file
    By marmotz, 4 years ago
  • No support for php5.3
    By marmotz, 4 years ago
  • Merge pull request #5 from stephpy/patch-1
    By marmotz, 4 years ago
  • fix .travis.yml to use new bin path
    By marmotz, 4 years ago
  • Add travis.yml to readme.
    By stephpy, 4 years ago
  • Merge pull request #2 from stephpy/avoid_usage_of_container
    By marmotz, 4 years ago
  • Container should be injected by user if it's useful, not all the time.
    By stephpy, 4 years ago
  • fix composer.json for the moment (hoa dependencies are broken)
    By marmotz, 4 years ago
  • Add travis configuration file
    By marmotz, 4 years ago
  • Comment typo
    By marmotz, 4 years ago
  • Add environment in start log
    By marmotz, 5 years ago
  • Refactor container part of Module and Runner classes
    By marmotz, 5 years ago