PHP Socket IO

Sheeraz Gul Sep 22, 2022
  1. PHP Socket IO
  2. How to Install Elephant IO
PHP Socket IO

This tutorial demonstrates how to implement socket.io in PHP.

PHP Socket IO

The socket.io library enables bidirectional, low-latency, and event-based communication between the client and the server. The official socket.io API can be found here, which is implemented using Node.js.

The socket.io is not directly implemented in PHP because it is not written in PHP. We can use the APIs like Elephant.io in PHP, which implements the socket.io in PHP for us.

The Elephant.io is a rough WebSocket client written in PHP, which can ease the communication between the server and client. The Elephant.io requires at least PHP 5.4 and OpenSSL.

The Elephant.io is licensed under the MIT license. The built-in engines for Elephant.io or Socket.io engines used in the Elephant.io are:

  1. Socket.io 5.x
  2. Socket.io 4.x
  3. Socket.io 3.x
  4. Socket.io 2.x
  5. Socket.io 1.x
  6. Socket.io 0.x

How to Install Elephant IO

The elephant.io is provided on GitHub and can be installed using the Composer. First of all, make sure the Composer is installed in your PHP and if it is not, first install the Composer, follow the steps described below.

  • Go to this link.
  • Click the Download option.
  • Download the Composer-Setup.exe file.
  • Run the Composer-Setup.exe file.
  • Install for all users.
  • Select the path to the php.exe file. Click Next.
  • Don’t select the proxy and click Next.
  • Click Install on the next page.
  • Once the installation is done, click Next and then Finish.

The above steps will install the Composer in our Windows system, and you can check it by running the composer command in cmd.

Now we need to run the composer.phar with the php command to install the elephant.io and to install the Composer.phar in the current directory; use the following command.

php -r "readfile('https://getcomposer.org/installer');" | php

The output for this command will be:

C:\Users\Sheeraz>php -r "readfile('https://getcomposer.org/installer');" | php
All settings correct for using Composer
Downloading...

Composer (version 2.4.2) successfully installed to: C:\Apache24\htdocs\composer.phar
Use it: php composer.phar

C:\Apache24\htdocs>

Once the Composer and Composer.phar is installed, let’s install the Elephant.io. Run the following command.

php composer.phar require elephantio/elephant.io

The above command will install the elephant.io and if it throws the version incompatibility error, then use the following command to directly install it from the composer.

composer require elephantio/elephant.io:*

The output for the Elephant IO installation will be:

PHP Elephant IO

Once the Elephant IO package is installed, we can use it to communicate with the socket server. Here is an example.

<?php
require( __DIR__ . '/elephantio/elephant.io/lib/ElephantIO/Client.php');
use ElephantIO\Client as ElephantIOClient;

$Elephant_Client = new ElephantIOClient('http://localhost:8000', 'socket.io', 1, false, true, true);

$Elephant_Client->init();
$Elephant_Client->emit('action', 'delftstack');
$Elephant_Client->close();

echo 'trying to send `delftstack` to the event named action';

?>

Before running this code, you might need to start the server at port 8000. Run the following command in cmd.

php -S localhost:8000

The code above will communicate with the socket server to send the data delftstack to the event named action. The output for the above code is:

trying to send `foo' to the event called action

If the communication takes more than 30 seconds, the code will throw a fatal error like:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Apache24\htdocs\vendor\elephantio\elephant.io\lib\ElephantIO\Client.php on line 326

The Elephant.io doesn’t contain documentation, but it provides a few examples from which we can understand the socket server communication; the examples can be found here.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook