All Projects → amphp → websocket-server

amphp / websocket-server

Licence: MIT license
WebSocket component for PHP based on the Amp HTTP server.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to websocket-server

Websocket Client
Async WebSocket client for PHP based on Amp.
Stars: ✭ 83 (+2.47%)
Mutual labels:  websockets, amphp
Websocket Client
WebSocket client for Python
Stars: ✭ 2,810 (+3369.14%)
Mutual labels:  websockets, rfc-6455
Websocat
Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
Stars: ✭ 3,477 (+4192.59%)
Mutual labels:  websockets, rfc-6455
diepssect
A public repo for hacky diep stuff - networking protocol, WebAssembly, memory editing, & physics
Stars: ✭ 26 (-67.9%)
Mutual labels:  websockets
spring-websocket-angular6
Example for using Spring Websocket and Angular with Stomp Messaging
Stars: ✭ 18 (-77.78%)
Mutual labels:  websockets
soketi
Just another simple, fast, and resilient open-source WebSockets server. 📣
Stars: ✭ 2,202 (+2618.52%)
Mutual labels:  websockets
STEWS
A Security Tool for Enumerating WebSockets
Stars: ✭ 154 (+90.12%)
Mutual labels:  websockets
racket-rfc6455
RFC 6455 WebSockets support for Racket.
Stars: ✭ 32 (-60.49%)
Mutual labels:  rfc-6455
raspivj
Turn the Raspberry Pi into a portable lightweight VJ plateform
Stars: ✭ 27 (-66.67%)
Mutual labels:  websockets
anycable-client
AnyCable / Action Cable JavaScript client for web, Node.js & React Native
Stars: ✭ 40 (-50.62%)
Mutual labels:  websockets
docs
The official soketi documentation. 📡
Stars: ✭ 55 (-32.1%)
Mutual labels:  websockets
py-graphql-client
Dead-simple GraphQL client with subscriptions over websockets
Stars: ✭ 36 (-55.56%)
Mutual labels:  websockets
btc-ticker-esp8266
realtime bitcoin price on 7 segment display with arduino on esp8266
Stars: ✭ 18 (-77.78%)
Mutual labels:  websockets
AmigoChat-Realtime-Chat-Application
AmigoChat is a responsive real-time chat application built on MERN Stack and Socket io.
Stars: ✭ 22 (-72.84%)
Mutual labels:  websockets
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-81.48%)
Mutual labels:  websockets
System.Net.WebSockets.Client.Managed
Microsoft's managed implementation of System.Net.WebSockets.ClientWebSocket tweaked for use on Windows 7 and .NET 4.5
Stars: ✭ 41 (-49.38%)
Mutual labels:  websockets
wocket
A WebSocket library for Deno
Stars: ✭ 103 (+27.16%)
Mutual labels:  websockets
channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-75.31%)
Mutual labels:  websockets
spring-boot-chatrooms
Spring Boot chatroom with multiple rooms using STOMP over websockets.
Stars: ✭ 33 (-59.26%)
Mutual labels:  websockets
bitmex-client-websocket
🛠️ C# client for Bitmex websocket API
Stars: ✭ 60 (-25.93%)
Mutual labels:  websockets

Websocket Server

This library provides a RequestHandler to easily handle Websocket connections using amphp/http-server.

Installation

This package can be installed as a Composer dependency.

composer require amphp/websocket-server

Documentation

The documentation for this library is currently a work in progress. Pull requests to improve the documentation are always welcome!

Requirements

  • PHP 7.2+

Example

<?php

// Note that this example requires:
// amphp/http-server-router
// amphp/http-server-static-content
// amphp/log

use Amp\Http\Server\HttpServer;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Amp\Http\Server\Router;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Loop;
use Amp\Promise;
use Amp\Socket\Server;
use Amp\Success;
use Amp\Websocket\Client;
use Amp\Websocket\Message;
use Amp\Websocket\Server\ClientHandler;
use Amp\Websocket\Server\Gateway;
use Amp\Websocket\Server\Websocket;
use Monolog\Logger;
use function Amp\ByteStream\getStdout;
use function Amp\call;

require __DIR__ . '/vendor/autoload.php';

$websocket = new Websocket(new class implements ClientHandler {
    private const ALLOWED_ORIGINS = [
        'http://localhost:1337',
        'http://127.0.0.1:1337',
        'http://[::1]:1337'
    ];
    
    public function handleHandshake(Gateway $gateway, Request $request, Response $response): Promise
    {
        if (!\in_array($request->getHeader('origin'), self::ALLOWED_ORIGINS, true)) {
            return $gateway->getErrorHandler()->handleError(403);
        }

        return new Success($response);
    }

    public function handleClient(Gateway $gateway, Client $client, Request $request, Response $response): Promise
    {
        return call(function () use ($gateway, $client): \Generator {
            while ($message = yield $client->receive()) {
                \assert($message instanceof Message);
                $gateway->broadcast(\sprintf(
                    '%d: %s',
                    $client->getId(),
                    yield $message->buffer()
                ));
            }
        });
    }
});

Loop::run(function () use ($websocket): Promise {
    $sockets = [
        Server::listen('127.0.0.1:1337'),
        Server::listen('[::1]:1337'),
    ];

    $router = new Router;
    $router->addRoute('GET', '/broadcast', $websocket);
    $router->setFallback(new DocumentRoot(__DIR__ . '/public'));

    $logHandler = new StreamHandler(getStdout());
    $logHandler->setFormatter(new ConsoleFormatter);
    $logger = new Logger('server');
    $logger->pushHandler($logHandler);

    $server = new HttpServer($sockets, $router, $logger);

    return $server->start();
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].