Zig
Zig
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const ws = try chilkat.WebSocket.init();
defer ws.deinit();
// For brevity, this example does not check for errors when etablishing the WebSocket connection.
// See Establish WebSocket Connection for more complete sample code for making the connection.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to wss://stream.binance.com:9443
rest.connect("stream.binance.com", 9443, true, false) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
ws.useConnection(rest) catch {
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
return;
};
ws.addClientHeaders() catch {};
// Raw streams are accessed at /ws/<streamName>
const response_body = rest.fullRequestNoBody(alloc, "GET", "/ws/btcusdt") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
ws.validateServerHandshake() catch {
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
std.debug.print("{s}\n", .{response_body});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
return;
};
std.debug.print("{s}\n", .{response_body});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
// POST JSON to subscribe to a stream
// {
// "method": "SUBSCRIBE",
// "params":
// [
// "btcusdt@aggTrade",
// "btcusdt@depth"
// ],
// "id": 1
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("method", "SUBSCRIBE") catch {};
json.updateString("params[0]", "btcusdt@aggTrade") catch {};
json.updateString("params[1]", "btcusdt@depth") catch {};
json.updateInt("id", 1) catch {};
// Send a full message in a single frame
const final_frame = true;
ws.sendFrame(try json.emit(alloc), final_frame) catch {
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
return;
};
const json_trade_data = try chilkat.JsonObject.init();
defer json_trade_data.deinit();
json_trade_data.setEmitCompact(false);
// Begin reading the trade stream response.
// We'll just read the 1st 10 updates and then exit..
var num_trades_received: i32 = 0;
while (num_trades_received < 5) {
ws.readFrame() catch {
std.debug.print("Failed to receive a frame\n", .{});
std.debug.print("ReadFrame fail reason = {d}\n", .{ws.getReadFrameFailReason()});
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
return;
};
// The responses we desire are in Text frames, where the opcode = 1.
if (ws.getFrameOpcodeInt() == 1) {
const received_json = try ws.getFrameData(alloc);
json_trade_data.load(received_json) catch {};
std.debug.print("{s}\n", .{try json_trade_data.emit(alloc)});
num_trades_received = num_trades_received + 1;
}
}
// Close the websocket connection.
ws.sendClose(true, 1000, "Closing this websocket.") catch {
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
return;
};
// Read the Close response.
ws.readFrame() catch {
std.debug.print("ReadFrame fail reason = {d}\n", .{ws.getReadFrameFailReason()});
std.debug.print("{s}\n", .{try ws.getLastErrorText(alloc)});
return;
};
std.debug.print("Success.\n", .{});
// The output of the above code is shown here:
}