Download the PHP package mita/uranus-socket-server without Composer

On this page you can find all versions of the php package mita/uranus-socket-server. 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 uranus-socket-server

Here is a revised and more structured version of the UranusSocketServer documentation, now including additional sections on configuring routes, creating controllers, and understanding packet structure after the connection is established:


UranusSocketServer

UranusSocketServer is a powerful, scalable, and easy-to-use PHP library designed for building high-performance WebSocket applications. With features like a flexible middleware pipeline, comprehensive event management, and efficient connection handling, UranusSocketServer empowers developers to create sophisticated WebSocket solutions effortlessly.

Features

Quick Start Guide

1. Installation

Install the library using Composer:

2. Configuration

2.1 Setting Up Routes

Create a routes.yaml file to define your WebSocket routes:

2.2 Creating a Controller

Create a controller to handle incoming WebSocket messages:

2.3 Understanding Packet Structure

When sending data to the server, the packet should be structured as a JSON object with at least two keys: route and msg.

Example Packet:

3. Running the Basic Chat Application Example

This section demonstrates how to quickly set up a simple WebSocket chat server.

3.1 Clone the Repository

3.2 Install Dependencies

3.3 Navigate to the Example Directory

3.4 Introduction to index.php

The index.php file is the entry point for the WebSocket server. It initializes the server with basic settings:

3.5 Run the WebSocket Server

Start the WebSocket server with:

3.6 Connect to the Server

Use a WebSocket client to connect to ws://127.0.0.1:7654 and send JSON packets like:

Join a Room:

Send a Message to the Room:

4. Running the ChatWithAuth Application Example

This example builds on the basic chat functionality by adding token-based authentication.

4.1 Clone the Repository

4.2 Install Dependencies

4.3 Navigate to the Example Directory

Navigate to the ChatWithAuth example directory:

4.4 Introduction to index.php

In this example, the index.php file registers an AuthPlugin for token-based authentication:

This example demonstrates how to create and integrate a custom plugin. By following this pattern, you can develop and register your plugins to extend the server’s capabilities.

4.5 Update the Secret Key

Replace 'your_secret_key' with your desired secret key for authentication.

4.6 Run the WebSocket Server

Start the WebSocket server:

4.7 Connect to the Server

Use a WebSocket client to connect to ws://127.0.0.1:7654, including the access_token in the URI query string.

Example Connection URI:

4.8 Join a Room

Authenticate and join a chat room with the following JSON payload:

4.9 Send a Message to the Room

Once authenticated and joined, send a message to the room:

4.10 Explanation of AuthPlugin.php

The AuthPlugin.php file is a custom plugin that adds token-based authentication to your WebSocket server. Below is a detailed explanation of its key parts:

Explanation: The constructor initializes the plugin with a secret key and a token parameter name. This setup allows you to easily configure which token to validate and how to validate it.

Explanation: The register method hooks the plugin into the WebSocket server by listening for specific events like middleware.register and connection.open. This is where the authentication logic gets integrated into the server's lifecycle.

Explanation: The onRegisterMiddleware method adds the authentication middleware to the pipeline. This ensures that each message passing through the server is checked for authentication.

Explanation: The onOpen method is invoked when a new WebSocket connection is opened. It extracts the token from the connection URI and validates it. If the token is invalid, the connection is closed immediately.

Explanation: The handle method processes incoming WebSocket messages. It checks the token within the message metadata, ensuring that only authenticated users can interact with the server.

Explanation: The validateToken method is a utility function that compares the provided token with the secret key. This simple method can be expanded for more complex validation logic if needed.

Code Overview for ChatWithAuth Example

API Documentation

1. SocketServer

The SocketServer class is the entry point for your WebSocket server.

2. RoutingMiddleware

The RoutingMiddleware class handles routing of incoming WebSocket messages.

3. MiddlewarePipeline

Processes messages through a series of middleware.

4. ConnectionManager

Manages WebSocket connections and their subscriptions.

5. EventDispatcher

Manages event listeners and dispatching events.

6. Packet

Represents a WebSocket message packet.

7. PacketFactory

Creates Packet instances from JSON strings.

8. PluginManager

Manages the lifecycle of plugins in the system.

Creating a Plugin

To create a plugin in UranusSocketServer, you need to implement the PluginInterface. A plugin can be used to add custom functionality to the server, such as authentication, logging, or other middleware. Here's a basic outline of how to create and register a plugin:

  1. Implement the PluginInterface:

  2. Add the Plugin to the Server:

By following this pattern, you can extend the functionality of your WebSocket server to fit your specific needs.

Event Documentation

Overview

The UranusSocketServer library provides a robust event-driven architecture, allowing you to hook into various lifecycle events of the WebSocket server. This section documents the events that are available, the context in which they are triggered, the parameters they provide, and examples of how to use them.

1. connection.opened

2. message.received

3. plugin.registered

4. connection.added

5. connection.removed

6. connection.subscribed

7. connection.unsubscribed

8. message.sent

Thank you for using UranusSocketServer! We hope it helps you build your next WebSocket application with ease. If you have any questions or need support, feel free to open an issue or reach out to the community.

Happy coding!


All versions of uranus-socket-server with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
cboden/ratchet Version ^0.4.3
symfony/routing Version ^5.0
php-di/php-di Version ^6.3
symfony/config Version ^5.4
symfony/yaml Version ^5.4
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 mita/uranus-socket-server contains the following files

Loading the files please wait ....