Giter VIP home page Giter VIP logo

php-wss's Introduction

php-wssc

Web-socket server/client with multi-process and parse templates support on server and send/receive options on client

Scrutinizer Code Quality Build Status License: MIT

Library comes with 5 main options

Server:

  • it`s a web-socket server for multiple connections with decoding/encoding for all events out of the box (with Dependency Injected MessageHandler)
  • it has GET uri parser, so You can easily use any templates
  • multiple process per user connections support, so You can fork processes to speed up performance deciding how many client-connections should be there

Client:

  • You have the ability to handshake (which is performed automatically) and send messages to server
  • Receive a response from the server

How do I get set up?

Preferred way to install is with Composer.

Just add

"require": {
  "arthurkushman/wssc": ">=1.3"  
}

in your projects composer.json.

OR

perform command in shell

$ composer require arthurkushman/wssc

Implement Your WebSocket handler class - ex.:

<?php
use WSSC\Contracts\ConnectionContract;
use WSSC\Contracts\WebSocket;
use WSSC\Exceptions\WebSocketException;

class ServerHandler extends WebSocket
{

    /*
     *  if You need to parse URI context like /messanger/chat/JKN324jn4213
     *  You can do so by placing URI parts into an array - $pathParams, when Socket will receive a connection 
     *  this variable will be appropriately set to key => value pairs, ex.: ':context' => 'chat'
     *  Otherwise leave $pathParams as an empty array
     */

    public $pathParams = [':entity', ':context', ':token'];
    private $clients = [];

    public function onOpen(ConnectionContract $conn)
    {
        $this->clients[] = $conn;
        echo 'Connection opend, total clients: ' . count($this->clients) . PHP_EOL;
    }

    public function onMessage(ConnectionContract $recv, $msg)
    {
        echo 'Received message:  ' . $msg . PHP_EOL;
        $recv->send($msg);
    }

    public function onClose(ConnectionContract $conn)
    {
        unset($this->clients[array_search($conn, $this->clients)]);
        $conn->close();
    }

    /**
     * @param ConnectionContract $conn
     * @param WebSocketException $ex
     */
    public function onError(ConnectionContract $conn, WebSocketException $ex)
    {
        echo 'Error occured: ' . $ex->printStack();
    }

    /**
     * You may want to implement these methods to bring ping/pong events
     * @param ConnectionContract $conn
     * @param string $msg
     */
    public function onPing(ConnectionContract $conn, $msg)
    {
        // TODO: Implement onPing() method.
    }

    /**
     * @param ConnectionContract $conn
     * @param $msg
     * @return mixed
     */
    public function onPong(ConnectionContract $conn, $msg)
    {
        // TODO: Implement onPong() method.
    }
}

Then put code bellow to Your CLI/Console script and run

$websocketServer = new WebSocketServer(new ServerHandler(), [
    'host' => '0.0.0.0',
    'port' => 8000
        ]);
$websocketServer->run(); 

How do I set WebSocket Client connection?

use WSSC\WebSocketClient;

$client = new WebSocketClient('ws://localhost:8000/notifications/messanger/yourtoken123');
$client->send('{"user_id" : 123}');
echo $client->receive();

That`s it, client is just sending any text content (message) to the Server.

Server reads all the messages and push them to Handler class, for further custom processing.

How to test

To run the Server - execute from the root of a project:

phpunit --bootstrap ./tests/_bootstrap.php ./tests/WebSocketServerTest.php

To run the Client - execute in another console:

phpunit --bootstrap ./tests/_bootstrap.php ./tests/WebSocketClientTest.php

PHP7 support since version 1.3 - with types, returns and better function implementations.

PS U'll see the processes increase named "php-wssc" as CPP (Connections Per-Process) will grow and decrease while stack will lessen. For instance, if set 100 CPP and there are 128 connections - You will be able to see 2 "php-wssc" processes with for ex.: ps aux | grep php-wssc

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.