Giter Club home page Giter Club logo

php-websocket's Introduction

Bloatless PHP WebSockets

Simple WebSocket server and client implemented in PHP.

About

This application is an extremely simple implementation of the WebSocket Protocol in PHP. It includes a server as well as a client. This implementation is optimal to get started with WebSockets and learn something. As soon as you want to create a full featured websocket based application you might want to switch to more sophisticated solution.

Installation

Clone or download the repository to your server. The package is also installable via composer running the following command:

composer require bloatless/php-websocket

Requirements

  • PHP >= 7.2

Hint: You can use version 1.0 if you're still on PHP5.

Usage

  • Adjust cli/server.php to your requirements.
  • Run: php cli/server.php

This will start a websocket server. (By default on localhost:8000)

Server example

This will create a websocket server listening on port 8000.

There a two applications registred to the server. The demo application will be available at ws://localhost:8000/demo and the status application will be available at ws://localhost:8000/status.

// Require neccessary files here...

$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000);

// Server settings:
$server->setMaxClients(100);
$server->setCheckOrigin(false);
$server->setAllowedOrigin('foo.lh');
$server->setMaxConnectionsPerIp(100);
$server->setMaxRequestsPerMinute(2000);

// Add your applications here:
$server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance());
$server->registerApplication('demo', \Bloatless\WebSocket\Application\DemoApplication::getInstance());

$server->run();

Client example

This creates a WebSocket cliente, connects to a server and sends a message to the server:

$client = new \Bloatless\WebSocket\Client;
$client->connect('127.0.0.1', 8000, '/demo', 'foo.lh');
$client->sendData([
    'action' => 'echo',
    'data' => 'Hello Wolrd!'
]);

Browser example

The repository contains two demo-pages to call in your browser. You can find them in the public folder. The index.html is a simple application which you can use to send messages to the server.

The status.html will display various server information.

php-websocket's People

Contributors

colindev avatar dominics avatar fushihara avatar joserolles avatar mazhack avatar nekudo avatar nicokaiser avatar olehs avatar

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.