C# WebSocket client library for real-time Forex, Cryptocurrency, and Stock market data from FCS API.
- Real-time WebSocket - Live price updates via WebSocket connection
- Multi-Market Support - Forex, Crypto, and Stock data
- Auto-Reconnect - Handles connection drops automatically
- Simple API - Easy to use with events
Clone the repository:
git clone https://github.com/fcsapi/websocket-csharp.git
cd websocket-csharp| Example | Description | Run Command |
|---|---|---|
| CryptoExample | Real-time crypto prices (BTC, ETH, BNB, SOL) | cd examples/CryptoExample && dotnet run |
| ForexExample | Real-time forex prices (EUR/USD, GBP/USD) | cd examples/ForexExample && dotnet run |
| StockExample | Real-time stock prices (AAPL, GOOGL, MSFT) | cd examples/StockExample && dotnet run |
| SimpleExample | Minimal quick start example | cd examples/SimpleExample && dotnet run |
using System;
using System.Collections.Generic;
using System.Text.Json;
using FcsApi;
class Program
{
const string ApiKey = "fcs_socket_demo";
static readonly string[] Symbols = { "BINANCE:BTCUSDT", "BINANCE:ETHUSDT", "BINANCE:BNBUSDT", "BINANCE:SOLUSDT" };
const string Timeframe = "1D";
static readonly Dictionary<string, PriceData> Prices = new();
static void Main(string[] args)
{
Console.WriteLine("\nConnecting to FCS WebSocket...");
Console.WriteLine("Press Ctrl+C to stop\n");
using var client = new FcsClient(ApiKey);
client.OnConnected += () =>
{
Console.WriteLine("Connected! Subscribing to crypto symbols...");
foreach (var symbol in Symbols)
client.Join(symbol, Timeframe);
};
client.OnMessage += (data) =>
{
var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type != "price") return;
if (!data.TryGetProperty("prices", out var prices)) return;
var symbol = data.TryGetProperty("symbol", out var s) ? s.GetString() : "";
var mode = prices.TryGetProperty("mode", out var m) ? m.GetString() : "";
if (mode == "initial" || mode == "candle")
{
var name = symbol.Contains(":") ? symbol.Split(':')[1] : symbol;
var close = prices.TryGetProperty("c", out var c) ? c.ToString() : "";
Console.WriteLine($" [{name}] Price: ${close}");
}
};
client.OnClose += (code, msg) => Console.WriteLine($"\n[!] Connection closed: {code} - {msg}");
client.OnError += (error) => Console.WriteLine($"\n[!] Error: {error.Message}");
client.Connect();
client.RunForever();
}
class PriceData
{
public string Open { get; set; }
public string High { get; set; }
public string Low { get; set; }
public string Close { get; set; }
public string Volume { get; set; }
public string Ask { get; set; }
public string Bid { get; set; }
}
}Use demo API key for testing: fcs_socket_demo
using FcsApi;
var client = new FcsClient("YOUR_API_KEY");
client.OnConnected += () => {
Console.WriteLine("Connected!");
client.Join("BINANCE:BTCUSDT", "1D");
};
client.OnMessage += (data) => {
Console.WriteLine(data);
};
client.Connect();
client.RunForever();using FcsApi;
var client = new FcsClient("fcs_socket_demo");
client.OnConnected += () => {
client.Join("BINANCE:BTCUSDT", "1D");
};
client.OnMessage += (data) => {
var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type == "price")
{
var symbol = data.GetProperty("symbol").GetString();
var price = data.GetProperty("prices").GetProperty("c").ToString();
Console.WriteLine($"{symbol}: ${price}");
}
};
client.Connect();
client.RunForever();using FcsApi;
var client = new FcsClient("fcs_socket_demo");
client.OnConnected += () => {
Console.WriteLine("Connected! Subscribing to forex pairs...");
client.Join("FX:EURUSD", "1D");
client.Join("FX:GBPUSD", "1D");
client.Join("FX:USDJPY", "1D");
};
client.OnMessage += (data) => {
var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type == "price")
{
var symbol = data.GetProperty("symbol").GetString();
var prices = data.GetProperty("prices");
var ask = prices.GetProperty("a").ToString();
var bid = prices.GetProperty("b").ToString();
Console.WriteLine($"{symbol}: Ask={ask} Bid={bid}");
}
};
client.Connect();
client.RunForever();using FcsApi;
var client = new FcsClient("fcs_socket_demo");
client.OnConnected += () => {
client.Join("BINANCE:ETHUSDT", "1D");
};
client.OnMessage += (data) => {
var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type == "price")
Console.WriteLine($"Price update: {data.GetProperty("symbol")}");
};
// Connect and run in background thread
client.Connect();
client.RunForever(blocking: false);
// Your other code continues here...
Console.WriteLine("Main thread continues...");
Thread.Sleep(60000);
client.Disconnect();using FcsApi;
var client = new FcsClient(apiKey, url: null);
client.ShowLogs = true; // Enable console logs (default: false)client.Connect(); // Connect to server
client.RunForever(blocking: true); // Start receiving (blocking=false for background)
client.Disconnect(); // Disconnect from serverclient.Join("BINANCE:BTCUSDT", "1D"); // Subscribe to symbol
client.Leave("BINANCE:BTCUSDT", "1D"); // Unsubscribe from symbol
client.RemoveAll(); // Unsubscribe from allclient.OnConnected += () => {
Console.WriteLine("Connected!");
};
client.OnMessage += (data) => {
Console.WriteLine(data);
};
client.OnClose += (code, msg) => {
Console.WriteLine($"Closed: {code}");
};
client.OnError += (error) => {
Console.WriteLine($"Error: {error.Message}");
};
client.OnReconnected += () => {
Console.WriteLine("Reconnected!");
};| Market | Format | Examples |
|---|---|---|
| Forex | FX:PAIR |
FX:EURUSD, FX:GBPUSD |
| Crypto | EXCHANGE:PAIR |
BINANCE:BTCUSDT, BINANCE:ETHUSDT |
| Stock | EXCHANGE:SYMBOL |
NASDAQ:AAPL, NYSE:TSLA |
| Timeframe | Description |
|---|---|
1 |
1 minute |
5 |
5 minutes |
15 |
15 minutes |
1H |
1 hour |
1D |
1 day |
1W |
1 week |
{
"type": "price",
"symbol": "BINANCE:BTCUSDT",
"timeframe": "1D",
"prices": {
"mode": "candle",
"t": 1766361600,
"o": 88658.87,
"h": 90588.23,
"l": 87900,
"c": 89962.61,
"v": 8192.70,
"a": 89962.62,
"b": 89962.61
}
}- Visit FCS API
- Sign up for free
- Get your API key
- Email: support@fcsapi.com
- Website: fcsapi.com
MIT License - see LICENSE file.