PHP code example of fcsapi / websocket

1. Go to this page and download the library: Download fcsapi/websocket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fcsapi / websocket example snippets


<!-- Include JS library from CDN -->
<script src="https://cdn.jsdelivr.net/gh/fcsapi/websocket-php/fcs-client-lib.js"></script>

<!-- Or local file -->
<script src="<?= $baseUrl 


$apiKey = 'YOUR_API_KEY'; // Or use 'fcs_socket_demo' for testing
$symbol = 'BINANCE:BTCUSDT';
$timeframe = '1D';

{{-- resources/views/realtime.blade.php --}}
@extends('layouts.app')

@section('scripts')
<script src="https://cdn.jsdelivr.net/gh/fcsapi/websocket-php/fcs-client-lib.js"></script>
@endsection

@section('content')
<div id="forex-price">Loading Forex...</div>
<div id="crypto-price">Loading Crypto...</div>

<script>
    const client = new FCSClient('{{ config("services.fcs.api_key") }}');

    client.onmessage = (data) => {
        if (data.type === 'price' && data.prices) {
            const p = data.prices;
            if (p.mode === 'candle' || p.mode === 'initial') {
                if (data.symbol.startsWith('FX:')) {
                    document.getElementById('forex-price').innerText =
                        `${data.symbol}: ${p.c}`;
                } else {
                    document.getElementById('crypto-price').innerText =
                        `${data.symbol}: $${p.c}`;
                }
            }
        }
    };

    client.connect().then(() => {
        client.join('FX:EURUSD', '1D');
        client.join('BINANCE:BTCUSDT', '1D');
    });
</script>
@endsection


// app/Controllers/Realtime.php
namespace App\Controllers;

class Realtime extends BaseController
{
    public function index()
    {
        $data = [
            'apiKey' => getenv('FCS_API_KEY') ?: 'fcs_socket_demo',
            'symbols' => ['FX:EURUSD', 'FX:GBPUSD', 'BINANCE:BTCUSDT']
        ];
        return view('realtime', $data);
    }
}

<!-- app/Views/realtime.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Real-time Prices</title>
    <script src="https://cdn.jsdelivr.net/gh/fcsapi/websocket-php/fcs-client-lib.js"></script>
</head>
<body>
    <table id="prices">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Open</th>
                <th>High</th>
                <th>Low</th>
                <th>Close</th>
                <th>Volume</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>

    <script>
        const client = new FCSClient('<?= esc($apiKey) 

'fcs' => [
    'api_key' => env('FCS_API_KEY', 'fcs_socket_demo'),
],
javascript
const client = new FCSClient('YOUR_API_KEY');
client.reconnectDelay = 5000;  // 5 seconds
client.reconnectlimit = 10;    // 10 attempts
client.focusTimeout = 5;       // 5 minutes