Download the PHP package r-martins/php-websocket without Composer

On this page you can find all versions of the php package r-martins/php-websocket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-websocket

Bloatless PHP WebSockets

Simple WebSocket server implemented in PHP.

Installation

Requirements

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:

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

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:

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:

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:

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:

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:

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


All versions of php-websocket with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
ext-json Version *
ext-sockets Version *
psr/log Version ^1.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package r-martins/php-websocket contains the following files

Loading the files please wait ....