A PHP Library for serving Multiple WebSocket Services in a single server
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/DiffSocket
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml

README.md

DiffSocket

Build Status

A PHP Library for serving multiple WebSocket services through a single port.

Normally, if you want to run multiple services, you would have to run WebSocket server on different ports. With DiffSocket, you can use a single port for different services.

Installation

composer require francium/diffsocket

Tutorial

Why don't I use different ports for different services ?

Some hosting providers don't allow you to bind on multiple ports, especially if you're using a Free plan. An example is OpenShift.

I created DiffSocket, because my WebSocket server is hosted on OpenShift and needed a way to serve multiple WebSocket services through a single port.

Demos

These different services are provided through a single WebSocket port (ws-subins.rhcloud.com:8000) :

Usage

Server

DiffSocket uses Ratchet for the WebSocket server. You should learn Ratchet to create services.

  • Configure server :

    <?php
    $DS = new Fr\DiffSocket(array(
      "server" => array(
        "host" => "127.0.0.1",
        "port" => "8000"
      )
    ));
  • To add a new service, create a class under namespace Fr\DiffSocket\Service

    SayHello.php

    namespace Fr\DiffSocket\Service;
    
    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;
    
    class SayHello implements MessageComponentInterface {
    
      public function onOpen(ConnectionInterface $conn){
        echo "New Connection - " . $conn->resourceId;
      }
    
      public function onClose(ConnectionInterface $conn){}
      public function onError(ConnectionInterface $conn, $error){}
    
      public function onMessage(ConnectionInterface $conn, $message){
        $conn->send("Hello");
      }
    
    }

    Then, you should register the service to DiffSocket by :

    $DS->addService("say-hello", "path/to/SayHello.php");
  • Then, add the code to run the server

    $DS->run();

You may also add services as an array when the object is made :

$DS = new Fr\DiffSocket(array(
  "server" => array(
    "host" => "127.0.0.1",
    "port" => "8000"
  ),
  "services" => array(
    "say-hello" => __DIR__ . "/services/SayHello.php",
    "chat" => __DIR__ . "/services/Chat.php",
    "game" => __DIR__ . "/services/GameServer.php"
  )
));

Client

Just add the service name in the URL as a GET parameter. Notice the use of / before ? :

ws://ws.example.com:8000/?service=say-hello
ws://ws.example.com:8000/?service=chat
ws://ws.example.com:8000/?service=game

An example in JavaScript :

var sayHelloWS = new WebSocket("ws://ws.example.com:8000/?service=say-hello");
var chatWS = new WebSocket("ws://ws.example.com:8000/?service=chat");
var gameWS = new WebSocket("ws://ws.example.com:8000/?service=game");

If the GET paramater service is not passed or the value passed to it doesn't match any available services, then DiffSocket would refuse the connection and close it.