Back to the full WebSockets listWhat are WebSockets?

php-websocket

GitHubStarsLast commitProject createdClosed vsOpen issues
388
3 days ago
8 years ago
2 / 0

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.

Requirements

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

Usage

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.