
Your First PHP WebSocket Client
29 April 2022
This tutorial is designed to get you started with WebSocket in PHP and show you how to receive real-time market data. We will use a simple editor and Composer package manager to make downloading the required packages simple.
Setup the Environment
For this tutorial you will need to have PHP installed, you can check if you already have it installed by opening command windows and typing "php.exe -version" if you get a version number output you can skip to the coding section, if not you will need to install PHP. You will also need an API key for the forex data service you can sign up for Free and get your streaming API key by starting a 14-day WebSocket trial from your user dashboard or a pre-populated code sample from our data docs page.
Setup PHP
Setting up PHP is very simple a can be done in 3 easy steps.
- Download the build file that contains php.exe from the PHP site and unpack the archive into a new directory.
- Update your "Environment Variables" or classpath to invoice the location of PHP.exe.
- To verify your setup, load a command window and run the following command, the output will be the version number of your PHP install.
//for windows php.exe -version // for linux php -version
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
Setup the project directory
Let's get started by creating a directory to put our source code and dependencies into. I have called this directory WebScoketClientPHP but you can call this anything you wish.
Now we need to install the Composer package manager. Once this has been established we can download the package we are going to need. Go to the directory created in the previous step and run the following command.
composer require textalk/websocket
Let's write some code
In your source directory create a new file called WebSocketClient.php and open this with your favorite development environment of text editor.
First, we will add the PHP tags and our require_once statement to import our packages from Composer.
<?php require_once("vendor/autoload.php"); ?>
Now we can create a new WebSocket passing in the URL for the WebSocket service as shown below. We assign this new WebSocket to a variable client.
$client = new WebSocketClient("wss://marketdata.tradermade.com/feedadv");
The WebSocket sends a "connected" message to the server so we need to listen for this and then send our login response with our ApiKey.
$message = $client->receive(); echo $message; $client->text("{"userKey":"YOUR_API_KEY", "symbol":"GBPUSD,EURUSD"}");
Now we are connected and have sent our connection string, we will now enter into a loop to keep listening for the forex price data.
while(true){ $message = $client->receive(); echo " Data ", $message, " "; }
We can run our program and look out at the output
php WebSocketClient.php
Voila! We are now streaming live forex rates.
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}
Now that we have the data we can look at parsing this into a usable format. First, we need to check if the data received is the "connected" message or a data item. Then we can parse the data and extract the elements.
if(strcmp($message,"connected") !== 0){ $decoded_json = json_decode($message); echo $decoded_json->symbol, $decode-json->ts, $decode_jdon->bid, $decode__json->ask, " "; }
You should now see the currency rates parsed 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
And we are done, you should now have a working WebSocket, If you have any questions please don't hesitate to get in touch via our contact page or our online chat.