Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

gnikolovski/websocket-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Websocket Server

This is a simple Websocket server written in PHP.

How to use it?

Clone this repo and use the Composer to install all dependencies:

composer install

Now you need to copy the existing .env.example file to .env file. Open it and change the default PORT variable if you want. This port must be added to the exception list in the firewall on your web server. For example, on Ubuntu:

sudo ufw allow 8080/tcp

Run the following command to start the server:

php websocket-server.php

Server can now accept connections and relay messages. To keep server running 24/7 you should probably use something like Supervisord

Client side

Open the console in your web browser, enter the following code and hit Enter:

var connection = new WebSocket('ws://IP_ADDRESS:PORT');

connection.onopen = function(e) {
    console.log("Connection established!");
};

connection.onmessage = function(e) {
    console.log(e.data);
};

You can now send messages:

connection.send('This is a message');

AUTHOR

Goran Nikolovski
Website: https://gorannikolovski.com
Email: goran@gorannikolovski.com

Releases

No releases published

Packages

No packages published

Languages