Create a C# app to subscribe live BTCUSDT data with WebSocket

Create a C# app to subscribe live BTCUSDT data with WebSocket

In this page, I will describe how to create an application using C# from Visual Code that connects to the Binance Test network, subscribes to BTCUSDT data, and tracks the price changes of the symbol.

If you need Binance Client Library to subscribe, follow Binance Client Library tutorial.

1. Prerequisites

Install:
• .NET SDK (7 or later recommended)
• Visual Studio Code
• VS Code extension: C# (by Microsoft)

Then verify your .Net version:

dotnet --version

2. Create Solution + Console Project

Open a terminal:

mkdir BinanceTracker
cd BinanceTracker

dotnet new sln -n BinanceTracker
dotnet new console -n BinanceTracker.App
dotnet sln add BinanceTracker.App

Open folder in VS Code:

code .

3. Add Required NuGet Packages

We’ll use:

• WebSocket client
• JSON parsing

Run:

cd BinanceTracker.App

dotnet add package System.Net.WebSockets.Client
dotnet add package Newtonsoft.Json

4. Understand Binance Testnet WebSocket

Binance Spot Testnet WebSocket base:

wss://testnet.binance.vision/ws

BTCUSDT trade stream:

btcusdt@trade

Full URL:

wss://testnet.binance.vision/ws/btcusdt@trade

5. Create Data Model Class

Create a file: Models/TradeMessage.cs

using Newtonsoft.Json;

public class TradeMessage
{
    [JsonProperty("e")]
    public string EventType { get; set; }

    [JsonProperty("E")]
    public long EventTime { get; set; }

    [JsonProperty("s")]
    public string Symbol { get; set; }

    [JsonProperty("p")]
    public string Price { get; set; }

    [JsonProperty("q")]
    public string Quantity { get; set; }
}

6. Create Binance WebSocket Service

Create: Services/BinanceSocketService.cs

using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class BinanceSocketService
{
    private readonly Uri _uri = new Uri("wss://testnet.binance.vision/ws/btcusdt@trade");

    public async Task StartAsync()
    {
        using var socket = new ClientWebSocket();

        Console.WriteLine("Connecting to Binance Testnet...");
        await socket.ConnectAsync(_uri, CancellationToken.None);

        Console.WriteLine("Connected.");

        var buffer = new byte[8192];

        while (socket.State == WebSocketState.Open)
        {
            var result = await socket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);

            var message = Encoding.UTF8.GetString(buffer, 0, result.Count);

            try
            {
                var trade = JsonConvert.DeserializeObject(message);

                if (trade != null)
                {
                    Console.WriteLine($"[{DateTime.Now}] {trade.Symbol} Price: {trade.Price} Qty: {trade.Quantity}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Parse error: " + ex.Message);
            }
        }
    }
}

7. Update Program.cs

Replace contents:

using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var service = new BinanceSocketService();
        await service.StartAsync();
    }
}

8. Run the Application

And finally run the application:

dotnet run

Contents related to 'Create a C# app to subscribe live BTCUSDT data with WebSocket'

Create a C# app to subscribe live BTCUSDT data with Binance Client Library
Create a C# app to subscribe live BTCUSDT data with Binance Client Library
How CSharp © 2007 Sitemap, Privacy Policy, Contact