stegru/websockets

WebSocket implementation

dev-master 2015-08-29 11:53 UTC

This package is not auto-updated.

Last update: 2025-09-13 23:49:20 UTC


README

#PHP WebSockets A PHP implementation of the WebSocket protocol.

##Quick Start

Install:

$ composer require stegru/websockets

Simple example:

<?php
require_once('vendor/autoload.php');

use WebSockets\Common\Event;
use WebSockets\Server\WebSocketServer;

$ws = new WebSocketServer();

$ws->addEventListener(function (Event $e) {

    switch ($e->eventId) {
        case WebSocketServer::EVENT_CONNECTED:
            $e->connection->sendMessage("Welcome!");
            break;

        case WebSocketServer::EVENT_MESSAGE:
            $e->connection->sendMessage(strrev($e->message));
            break;
    }
});

$ws->start();

Run:

$ php ws.php 

Browser:

var ws = new WebSocket("ws://localhost:8088/socketserver", "hello");
ws.onmessage = function(e) { console.log(e.data); }
ws.send("hello!");

Output:

Welcome!
!olleh