Go to file
Chris Boden b16438a8a0 Merge pull request #19 from igorw/patch-2
[composer] Require symfony 2.1.* instead of dev-master
2012-05-01 10:34:36 -07:00
src/Ratchet [WAMP] unit tests and fix 2012-04-29 19:21:29 -04:00
tests Unit tests 2012-04-29 20:42:33 -04:00
.gitignore Working Composer 2012-01-30 21:41:49 -05:00
.travis.yml Updated CI conf 2012-04-16 18:39:06 -04:00
composer.json [composer] Require symfony 2.1.* instead of dev-master 2012-05-01 20:03:02 +03:00
composer.lock Deps revert 2012-04-27 09:16:51 -04:00
LICENSE Moar cleaning 2011-11-01 11:01:43 -04:00
phpunit.xml.dist WAMP incoming functionality 2012-01-13 23:46:30 -05:00
README.md Updating readme 2012-04-29 20:50:44 -04:00

Build Status

#Ratchet

A PHP 5.3 (PSR-0 compliant) component library for serving sockets and building socket based applications. Build up your application through simple interfaces using the decorator and command patterns. Re-use your application without changing any of its code just by combining different components.

##WebSockets

  • Supports the RFC6455, HyBi-10, and Hixie76 protocol versions (at the same time)
  • Tested on Chrome 18 - 16, Firefox 6 - 8, Safari 5, iOS 4.2, iOS 5

##Requirements

Shell access is required and a dedicated machine with root access is recommended. To avoid proxy/firewall blockage it's recommended WebSockets are run on port 80, which requires root access. Note that you can not run two applications (Apache and Ratchet) on the same port, thus the requirement for a separate machine (for now).

Cookies from your domain will be passed to the socket server, allowing you to identify users. Accessing your website's session data in Ratchet requires you to use Symfony2 HttpFoundation Sessions on your website.

Documentation

User and API documentation is available on Ratchet's website: http://socketo.me

See https://github.com/cboden/Ratchet-examples for some out-of-the-box working demos using Ratchet.


###A quick server example

<?php
namespace MyApps;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Component\Server\IOServerComponent;
use Ratchet\Component\WebSocket\WebSocketComponent;
use Ratchet\Resource\Command\Composite as Cmds;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Resource\Command\Action\CloseConnection;

/**
 * chat.php
 * Send any incoming messages to all connected clients (except sender)
 */
class Chat implements MessageComponentInterface {
    protected $_clients;

    public function __construct(MessageComponentInterface $app = null) {
        $this->_clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->_clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $commands = new Cmds;

        foreach ($this->_clients as $client) {
            if ($from != $client) {
                $msg_cmd = new SendMessage($client);
                $msg_cmd->setMessage($msg);

                $commands->enqueue($msg_cmd);
            }
        }

        return $commands;
    }

    public function onClose(ConnectionInterface $conn) {
        $this->_clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        return new CloseConnection($conn);
    }
}

// Run the server application through the WebSocket protocol
$server = new IOServerComponent(new WebSocketComponent(new Chat));
$server->run(8000);
# php chat.php