DEV Community

Cover image for Your First PHP WebSocket Client
Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on • Updated on

Your First PHP WebSocket Client

This tutorial will guide you through setting up WebSocket in PHP and demonstrate how to acquire real-time market data. We will utilise a straightforward editor and the Composer package management to simplify the process of obtaining the necessary programmes.

Please sign-up for free to access real-time and historical Forex, CFD, and Crypto Data. TraderMade offers 1000 free requests every month. Also, you can self-start a 14-day WebSocket Trial once you log in.

Environment setup.
To determine whether PHP is already available, open a command window and type "php.exe -version"; if a version number returns, you can move on to the coding part; otherwise, you must install PHP. Additionally, you require an API key for the FX data service. Register for Free, obtain your streaming API key and start a 14-day WebSocket trial from your user dashboard. Otherwise, use a pre-populated code sample from our data docs page.

Setting up PHP
Three simple steps are all that is required to set up PHP.

  1. Download the php.exe build file from the PHP website and unpack it into a new directory.

  2. Update your classpath or "Environment Variables" to reflect PHP.exe's location.

  3. The output of the following command, which you may use to check your setup, will show the version number of your PHP installation.

//for windows
php.exe -version
// for linux
php -version
Enter fullscreen mode Exit fullscreen mode

expected output.

PHP 7.2.24-0ubuntu0.18.04.13 (cli) (built: Jul  6 2022 12:23:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-0ubuntu0.18.04.13, Copyright (c) 1999-2018, by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

Make the project directory settings.
Let's begin by making a directory for our source code and dependencies. Although I've given this directory the name WebScoketClientPHP, you can give it any name you choose.

Installing the Composer package manager is the next step. Once this is confirmed, we can download the package we will require. Run the following command in the directory you created in the previous step.

composer require textalk/websocket
Enter fullscreen mode Exit fullscreen mode

Write some code, shall we?
Please make a new file called WebSocketClient.php in your source directory and then open it in your preferred text editor development environment.

We will first add our required statement and PHP tags before importing our packages from Composer.

<?php>
  require_once("vendor/autoload.php");
<?>
Enter fullscreen mode Exit fullscreen mode

As seen below, we may build a new WebSocket by providing the URL for the WebSocket service. We can assign this fresh WebSocket to a client variable.

$client = new WebSocketClient("wss://marketdata.tradermade.com/feedadv");
Enter fullscreen mode Exit fullscreen mode

We must wait for the WebSocket to provide a "connected" message to the server before sending our login response with the API Key.

$message =  $client->receive();
echo $message;
$client->text("{"userKey":"YOUR_API_KEY", "symbol":"GBPUSD,EURUSD"}");
Enter fullscreen mode Exit fullscreen mode

Once we have established a connection and submitted our connection string, we will immediately enter into a loop to continuously check for updates on forex price data.

while(true){
   $message =  $client->receive();
   echo " Data ", $message, "
";
}
Enter fullscreen mode Exit fullscreen mode

We may run our software and review the results.

php WebSocketClient.php
Enter fullscreen mode Exit fullscreen mode

Voila! Right now, you can see live currency rates streaming.

Data {"symbol":"GBPUSD","ts":"1651244018768","bid":1.25486,"ask":1.25486,"mid":1.25486}
Data {"symbol":"GBPUSD","ts":"1651244018790","bid":1.25486,"ask":1.25488,"mid":1.2548699}
Data {"symbol":"GBPUSD","ts":"1651244018791","bid":1.25482,"ask":1.25488,"mid":1.2548499}
Data {"symbol":"GBPUSD","ts":"1651244018796","bid":1.25487,"ask":1.25488,"mid":1.254875}
Data {"symbol":"GBPUSD","ts":"1651244018815","bid":1.25485,"ask":1.25488,"mid":1.2548649}
Data {"symbol":"GBPUSD","ts":"1651244018816","bid":1.25485,"ask":1.25486,"mid":1.254855}
Data {"symbol":"GBPUSD","ts":"1651244018842","bid":1.25485,"ask":1.25487,"mid":1.25486}
Data {"symbol":"GBPUSD","ts":"1651244018866","bid":1.25485,"ask":1.25486,"mid":1.254855}
Data {"symbol":"GBPUSD","ts":"1651244018868","bid":1.25484,"ask":1.25486,"mid":1.25485}
Enter fullscreen mode Exit fullscreen mode

Now that we have the data, we may consider processing it into a usable format. First, we must determine whether the data received is a data item or the "connected" message. We can then parse the elements from the data by parsing it.

if(strcmp($message,"connected") !== 0){
        $decoded_json = json_decode($message);
        echo $decoded_json->symbol, $decode-json->ts, $decode_jdon->bid, $decode__json->ask, "
";
}
Enter fullscreen mode Exit fullscreen mode

You can view currency rates displayed in real time.

EURUSD 1651244529366 1.05515 1.05517
GBPUSD 1651244529371 1.2554 1.2554
GBPUSD 1651244529385 1.2554 1.25541
EURUSD 1651244529389 1.05515 1.05516
EURUSD 1651244529416 1.05518 1.05519
GBPUSD 1651244529455 1.25542 1.25543
GBPUSD 1651244529458 1.25542 1.25546
GBPUSD 1651244529492 1.2554 1.25546
GBPUSD 1651244529496 1.2554 1.25545
Enter fullscreen mode Exit fullscreen mode

And that's it; your WebSocket should now be operational. If you have any issues, don't hesitate to reach us through our contact page or online chat.

TraderMade provides reliable and accurate Forex data via Forex API. You can sign up for a free API key and start exploring real-time and historical data at your fingertips.

Top comments (0)