We’re here to help and answer any question you might have. We look forward to hearing from you.

Ratchet is a popular PHP library that allows you to create WebSocket applications and servers easily. With Ratchet, you can build real-time, interactive, and event-driven applications using the WebSocket protocol.
apache

Configure Ratchet socket in Apache server

dastek logo

What will you do

  1. Install php-ratchet-package

Requirements

  1. Apache Web Server.
  2. PHP (version 5.4 or higher).
  3. Composer (dependency manager for PHP).

Step 1: Install php-ratchet-package

Command 1php ~/composer.phar require cboden/ratchet

Step 2: Add following lines to your composer.json file

Command 1{
“autoload”: {
“psr-4”: {
“MyApp\\”: “bin”
}
},
“require”: {
“cboden/ratchet”: “^0.4”
}
}

Step 3: Create a new folder named bin and a file inside named Chat.php with the following class

Command 1

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
}

public function onMessage(ConnectionInterface $from, $msg) {
}

public function onClose(ConnectionInterface $conn) {
}

public function onError(ConnectionInterface $conn, \Exception $e) {
}
}

Step 4: Now in folder src there will be a file server.php if not create in same directory with follwoing lines of code

Command 1

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

require dirname(__DIR__) . ‘/vendor/autoload.php’;

$server = IoServer::factory(
new Chat(),
8080
);

$server->run();

Step 5: Create a demo php file to test the websocket connection

Command 1var conn = new WebSocket(‘ws://localhost:8080’);
conn.onopen = function(e) {
console.log(“Connection established!”);
};

Step 6: Open port 8080 allow incoming connections from anywhere.

Step 7: For wss over SSL

Go to your server terminal
Enable following modules & restart the server-

Command 1sudo a2enmod proxy
Command 2sudo a2enmod proxy_http
Command 3sudo a2enmod rewrite
Command 4sudo systemctl apache2 restart

Make sure above modules got enabled
 And edit proxy.conf file –

Command 1sudo vim /etc/apache2/mods-enabled/proxy.conf
Command 2ProxyPass /wss ws://localhost:8080


Exit and restart the server by 

Command 1sudo systemctl apache2 restart


Change your demo php file with following code

Command 1var conn = new WebSocket(‘wss://domain.com/wss’);
conn.onopen = function(e) {
console.log(“Connection established!”);
};

Trust and Worth

Our Customers

We are having a diversified portfolio and serving customers in the domains namely Sports Management, Online Laundry System, Matrimonial, US Mortgage, EdTech and so on.

Would you like to start a project with us?

DAStek team would be happy to hear from you and would love to turn your ‘Imaginations to Reality’.