Sample code for 30+ languages & platforms
Lua

WebSocket Binance Trade Stream (subscribe and receive updates)

See more WebSocket Examples

Subscribe to a binance trade stream and receive updates.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local ws = chilkat.WebSocket.new()

-- 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.

local rest = chilkat.Rest.new()

-- Connect to wss://stream.binance.com:9443
local success = rest:connect("stream.binance.com", 9443, true, false)
if not success then
    print(rest.last_error_text)
    return
end

success = ws:use_connection(rest)
if not success then
    print(ws.last_error_text)
    return
end

ws:add_client_headers()

--  Raw streams are accessed at /ws/<streamName>
local responseBody = rest:full_request_no_body("GET", "/ws/btcusdt")
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

success = ws:validate_server_handshake()
if not success then
    print(ws.last_error_text)
    print(responseBody)
    print(rest.response_header)
    return
end

print(responseBody)
print(rest.response_header)

-- POST JSON to subscribe to a stream

-- {
-- "method": "SUBSCRIBE",
-- "params":
-- [
-- "btcusdt@aggTrade",
-- "btcusdt@depth"
-- ],
-- "id": 1
-- }

local json = chilkat.JsonObject.new()
json:update_string("method", "SUBSCRIBE")
json:update_string("params[0]", "btcusdt@aggTrade")
json:update_string("params[1]", "btcusdt@depth")
json:update_int("id", 1)

-- Send a full message in a single frame
local finalFrame = true
success = ws:send_frame(json:emit(), finalFrame)
if not success then
    print(ws.last_error_text)
    return
end

local jsonTradeData = chilkat.JsonObject.new()
jsonTradeData.emit_compact = false

-- Begin reading the trade stream response.
-- We'll just read the 1st 10 updates and then exit..
local receivedFinalFrame = false
local numTradesReceived = 0
while numTradesReceived < 5 do
    success = ws:read_frame()
    if not success then
        print("Failed to receive a frame")
        print("ReadFrame fail reason = " .. ws.read_frame_fail_reason)
        print(ws.last_error_text)
        return
    end

    -- The responses we desire are in Text frames, where the opcode = 1.
    if ws.frame_opcode_int == 1 then
        local receivedJson = ws:get_frame_data()

        jsonTradeData:load(receivedJson)
        print(jsonTradeData:emit())

        numTradesReceived = numTradesReceived + 1
    end
end

-- Close the websocket connection.
success = ws:send_close(true, 1000, "Closing this websocket.")
if not success then
    print(ws.last_error_text)
    return
end

-- Read the Close response.
success = ws:read_frame()
if not success then
    print("ReadFrame fail reason = " .. ws.read_frame_fail_reason)
    print(ws.last_error_text)
    return
end

print("Success.")

-- The output of the above code is shown here: