Crypto Websocket Extensions
🧰 Unified and optimized data structures across cryptocurrency exchanges
Install / Use
npx skills add Marfusios/crypto-websocket-extensionsInstalls into whichever agent you are using.
README

Cryptocurrency websocket extensions
This is a library that provides extensions to cryptocurrency websocket exchange clients.
It helps to unify data models and usage of more clients together.
License:
Apache License 2.0
Features
- installation via NuGet
- full (with all exchange clients) - Crypto.Websocket.Extensions
- core (only interfaces and features) - Crypto.Websocket.Extensions.Core
- targets
netstandard2.1,net6.0,net7.0,net8.0,net9.0,net10.0 - built on Websocket.Client 5.4.0 through the updated exchange clients
- third-party exchange adapters for Bybit, Luno, and VALR remain enabled; NuGet resolves the shared websocket transport to the newer package version
- benchmarked order book hot paths with BenchmarkDotNet
- reactive extensions (Rx.NET)
- integrated logging abstraction (LibLog)
Performance
The order book implementation is tuned for allocation-sensitive websocket streams. Common L2 diff processing avoids temporary notification objects when nobody is subscribed, keeps internal source-to-orderbook handoff on the single-update path, and uses list-based dispatch for bulk level updates to avoid interface enumerator allocations.
The current benchmark suite focuses on CryptoOrderBook, CryptoOrderBookL2, and related source adapters. In the latest pass, representative BenchmarkDotNet runs showed CryptoOrderBook.BidLevels improving from 17,822 ns / 77 KB to 5,409 ns / 4.8 KB, and CryptoOrderBookL2 diff processing improving from 935 ns / 545 B to 618 ns / 161 B. See the benchmarks README for commands and detailed results.
Supported exchanges
| Logo | Name | Websocket client |
| ------------- | ------------- |:------:|
|
| Bitfinex | bitfinex-client-websocket |
|
| BitMEX | bitmex-client-websocket |
|
| Binance | binance-client-websocket |
|
| Coinbase | coinbase-client-websocket |
|
| Bitstamp | bitstamp-client-websocket |
Extensions
Order book
- efficient data structure, based on howtohft blog post
CryptoOrderBookclass - unified order book across all exchanges- support for L2 (grouped by price), L3 (every single order) market data
- support for snapshots and deltas/diffs
- provides streams:
OrderBookUpdatedStream- streams on an every order book updateBidAskUpdatedStream- streams when bid or ask price changed (top level of the order book)TopLevelUpdatedStream- streams when bid or ask price/amount changed (top level of the order book)
- provides properties and methods:
BidLevelsandAskLevels- ordered array of current state of the order bookBidLevelsPerPriceandAskLevelsPerPrice- dictionary of all L3 orders split by priceFindLevelByPriceandFindLevelById- returns specific order book level
Usage:
var url = BitmexValues.ApiWebsocketUrl;
var communicator = new BitmexWebsocketCommunicator(url);
var client = new BitmexWebsocketClient(communicator);
var pair = "XBTUSD";
var source = new BitmexOrderBookSource(client);
var orderBook = new CryptoOrderBook(pair, source);
// orderBook.BidAskUpdatedStream.Subscribe(xxx)
orderBook.OrderBookUpdatedStream.Subscribe(quotes =>
{
var currentBid = orderBook.BidPrice;
var currentAsk = orderBook.AskPrice;
var bids = orderBook.BidLevels;
// xxx
});
await communicator.Start();
Trades
ITradeSource- unified trade info stream across all exchanges
Orders (authenticated)
CryptoOrdersclass - unified orders status across all exchanges with features:- orders view and searching - only executed, search by id, client id, etc.
- our vs all orders - using client id prefix to distinguish between orders
Position (authenticated)
IPositionSource- unified position info stream across all exchanges
Wallet (authenticated)
IWalletSource- unified wallet status stream across all exchanges
More usage examples:
Pull Requests are welcome!
Powerfull Rx.NET
Don't forget that you can do pretty nice things with reactive extensions and observables. For example, if you want to check latest bid/ask prices from all exchanges all together, you can do something like this:
Observable.CombineLatest(new[]
{
bitmexOrderBook.BidAskUpdatedStream,
bitfinexOrderBook.BidAskUpdatedStream,
binanceOrderBook.BidAskUpdatedStream,
})
.Subscribe(HandleQuoteChanged);
// Method HandleQuoteChanged(IList<CryptoQuotes> quotes)
// will be called on every exchange's price change
Multi-threading
Observables from Reactive Extensions are single threaded by default. It means that your code inside subscriptions is called synchronously and as soon as the message comes from websocket API. It brings a great advantage of not to worry about synchronization, but if your code takes a longer time to execute it will block the receiving method, buffer the messages and may end up losing messages. For that reason consider to handle messages on the other thread and unblock receiving thread as soon as possible. I've prepared a few examples for you:
Default behavior
Every subscription code is called on a main websocket thread. Every subscription is synchronized together. No parallel execution. It will block the receiving thread.
client
.Streams
.TradesStream
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called in a correct order, according to websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 -----
Parallel subscriptions
Every single subscription code is called on a separate thread. Every single subscription is synchronized, but different subscriptions are called in parallel.
client
.Streams
.TradesStream
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called in parallel, do not follow websocket flow
// ----- code1 ----- code1 ----- code1 -----
// ----- code2 code2 ----- code2 code2 code2
Parallel subscriptions with synchronization
In case you want to run your subscription code on the separate thread but still want to follow websocket flow through every subscription, use synchronization with gates:
private static readonly object GATE1 = new object();
client
.Streams
.TradesStream
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called concurrently and follow websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 ----
Async/Await integration
Using async/await in your subscribe methods is a bit tricky. Subscribe from Rx.NET doesn't await tasks,
so it won't block stream execution and cause sometimes undesired concurrency. For example:
client
.Streams
.TradesStream
.Subscribe(async trade => {
// do smth 1
await Task.Delay(5000); //
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
blender-python-addon
40.5kBlender Python add-on rules for operators, panels, properties, registration, testing, and API-safe scripting
flutter-development-guidelines-cursorrules-prompt-file
40.5kCursor rules for Flutter development with MVVM architecture, Riverpod state management, Material widgets, and Dart style guidelines.
commit-push-pr
140.6kCommit, push, and open a PR
