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.
Configure Ratchet socket in Apache server
What will you do
- Install php-ratchet-package
Requirements
- Apache Web Server.
- PHP (version 5.4 or higher).
- Composer (dependency manager for PHP).
Step 1: Install php-ratchet-package
Command 1 | php ~/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 class Chat implements MessageComponentInterface { 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 require dirname(__DIR__) . ‘/vendor/autoload.php’; $server = IoServer::factory( $server->run(); |
Step 5: Create a demo php file to test the websocket connection
Command 1 | var 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 1 | sudo a2enmod proxy |
Command 2 | sudo a2enmod proxy_http |
Command 3 | sudo a2enmod rewrite |
Command 4 | sudo systemctl apache2 restart |
Make sure above modules got enabled
And edit proxy.conf file –
Command 1 | sudo vim /etc/apache2/mods-enabled/proxy.conf |
Command 2 | ProxyPass /wss ws://localhost:8080 |
Exit and restart the server by
Command 1 | sudo systemctl apache2 restart |
Change your demo php file with following code
Command 1 | var 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’.