All Projects → bloatless → Php Websocket

bloatless / Php Websocket

Licence: mit
Simple WebSocket server implemented in PHP.

Projects that are alternatives of or similar to Php Websocket

channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-95.77%)
Mutual labels:  websocket-server, websockets
text
An experiment with WebSockets and the human condition.
Stars: ✭ 51 (-89.22%)
Mutual labels:  websocket-server, websockets
remoting
Jetlang Remoting - asynchronous distributed messaging
Stars: ✭ 27 (-94.29%)
Mutual labels:  websocket-server, websockets
Microwebsrv
A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
Stars: ✭ 420 (-11.21%)
Mutual labels:  websockets, websocket-server
Beast
HTTP and WebSocket built on Boost.Asio in C++11
Stars: ✭ 3,241 (+585.2%)
Mutual labels:  websockets, websocket-server
Websocat
Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
Stars: ✭ 3,477 (+635.1%)
Mutual labels:  websockets, websocket-server
apollo-chat-graphql-server
Apollo Chat is a Chat Service build on GraphQL Apollo with Subscriptions
Stars: ✭ 13 (-97.25%)
Mutual labels:  websocket-server, websockets
Php Wss
Web-socket server/client with multi-process and parse templates support on server and send/receive options on client
Stars: ✭ 117 (-75.26%)
Mutual labels:  websockets, websocket-server
LazWebsockets
Websocket Server and Client Library written in Lazarus
Stars: ✭ 51 (-89.22%)
Mutual labels:  websocket-server, websockets
reactive-streams-for-java-developers
No description or website provided.
Stars: ✭ 16 (-96.62%)
Mutual labels:  websocket-server, websockets
Ixwebsocket
websocket and http client and server library, coming with ws, a command line swiss army knife utility
Stars: ✭ 204 (-56.87%)
Mutual labels:  websockets, websocket-server
Websockets
Library for building WebSocket servers and clients in Python
Stars: ✭ 3,724 (+687.32%)
Mutual labels:  websockets, websocket-server
Ws Tcp Relay
A simple relay between WebSocket clients and TCP servers
Stars: ✭ 186 (-60.68%)
Mutual labels:  websockets, websocket-server
Websocketd
Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
Stars: ✭ 15,828 (+3246.3%)
Mutual labels:  websockets, websocket-server
Websocket
WSServer is a fast, configurable, and extendable WebSocket Server for UNIX systems written in C (C11).
Stars: ✭ 144 (-69.56%)
Mutual labels:  websockets, websocket-server
simple-websocket-server
A simple WebSocket server
Stars: ✭ 26 (-94.5%)
Mutual labels:  websocket-server, websockets
Sandstone
PHP microframework designed to build a RestApi working together with a websocket server. Build a real time RestApi!
Stars: ✭ 98 (-79.28%)
Mutual labels:  websockets, websocket-server
Facil.io
Your high performance web application C framework
Stars: ✭ 1,393 (+194.5%)
Mutual labels:  websockets, websocket-server
node-jsonrpc2
JSON-RPC 2.0 server and client library, with HTTP (with Websocket support) and TCP endpoints
Stars: ✭ 103 (-78.22%)
Mutual labels:  websocket-server, websockets
Microwebsrv2
The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
Stars: ✭ 295 (-37.63%)
Mutual labels:  websockets, websocket-server

Bloatless PHP WebSockets

Simple WebSocket server implemented in PHP.

Installation

Requirements

  • PHP >= 7.4
  • ext-json
  • ext-sockets

Installation procedure

Install the package using composer:

composer require bloatless/php-websocket

Usage

Server

After downloading the sourcecode to your machine, you need some code to actually put your websocket server together. Here is a basic exmaple:

<?php

// require necessary files here

// create new server instance
$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000, '/tmp/phpwss.sock');

// server settings
$server->setMaxClients(100);
$server->setCheckOrigin(false);
$server->setAllowedOrigin('example.com');
$server->setMaxConnectionsPerIp(20);

// add your applications
$server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance());
$server->registerApplication('chat', \Bloatless\WebSocket\Examples\Application\Chat::getInstance());

// start the server
$server->run();

Assuming this code is in a file called server.php you can then start your server with the following command:

php server.php

The websocket server will then listen for new connections on the provided host and port. By default, this will be localhost:8000.

This repositoy also includes a working example in examples/server.php

Applications

The websocket server itself handles connections but is pretty useless without any addional logic. This logic is added by applications. In the example above two applications are added to the server: status and chat.

The most important methods in your application will be:

interface ApplicationInterface
{
    public function onConnect(Connection $connection): void;

    public function onDisconnect(Connection $connection): void;

    public function onData(string $data, Connection $client): void;

    public function onIPCData(array $data): void;
}

onConnect and onDisconnect can be used to keep track of all the clients connected to your application. onData will be called whenever the websocket server receives new data from one of the clients connected to the application. onIPCData will be called if data is provided by another process on your machine. (See Push-Client (IPC))

A working example of an application can be found in examples/Application/Chat.php

Timers

A common requirement for long-running processes such as a websocket server is to execute tasks periodically. This can be done using timers. Timers can execute methods within your server or application periodically. Here is an example:

$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000, '/tmp/phpwss.sock');
$chat = \Bloatless\WebSocket\Examples\Application\Chat::getInstance();
$server->addTimer(5000, function () use ($chat) {
    $chat->someMethod();
});
$server->registerApplication('chat', $chat);

This example would call the method someMethod within your chat application every 5 seconds.

Push-Client (IPC)

It is often required to push data into the websocket-server process from another application. Let's assume you run a website containing a chat and an area containing news or a blog. Now every time a new article is published in your blog you want to notify all users currently in your chat. To achieve this you somehow need to push data from your blog logic into the websocket server. This is where the Push-Client comes into play.

When starting the websocket server, it opens a unix-domain-socket and listens for new messages. The Push-Client can then be used to send these messages. Here is an example:

$pushClient = new \Bloatless\WebSocket\PushClient('//tmp/phpwss.sock');
$pushClient->sendToApplication('chat', [
    'action' => 'echo',
    'data' => 'New blog post was published!',
]);

This code pushes data into your running websocket-server process. In this case the echo method within the chat-application is called and it sends the provided message to all connected clients.

You can find the full working example in: examples/push.php

Important Hint: Push messages cannot be larger than 64kb!

Client (Browser/JS)

Everything above this point was related to the server-side of things. But how to connect to the server from your browser?

Here is a simple example:

<script>
 // connect to chat application on server
let serverUrl = 'ws://127.0.0.1:8000/chat';
let socket = new WebSocket(serverUrl);

// log new messages to console
socket.onmessage = (msg) => {
    let response = JSON.parse(msg.data);
    console.log(response.data);
};
</script>

This javascript connects to the chat application on your server and prints all incoming messages into the console.

A better example of the chat client can be found in: examples/public/chat.html

Intended use and limitations

This project was mainly built for educational purposes. The code is relatively simple and easy to understand. This server was not tested in production, so I strongly recommend not to use it in a live project. It should be totally fine for small educational projects or internal tools, but most probably will not handle huge amounts of traffic or connections very well.

Also, some "features" are missing by design:

  • SSL is not supported. If required, you can use a reverse proxy like nginx.
  • Binary messages are not supported.
  • A lot of other stuff I did not even realize ;)

In case you need a more "robust" websocket server written in PHP, please have a look at the excellent alternatives listed below.

Alternatives

License

MIT

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].