Sample code for 30+ languages & platforms
Lua

Read Bitfinex WebSocket Ticker Channel

See more WebSocket Examples

Subscribes to the public Bitfinex websocket ticker channel and receives ticker 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 api.bitfinex.com
local success = rest:connect("api.bitfinex.com", 443, true, false)
ws:use_connection(rest)
ws:add_client_headers()

local responseBody = rest:full_request_no_body("GET", "/ws")
success = ws:validate_server_handshake()
if not success then
    print(ws.last_error_text)
    print(responseBody)
    print(rest.response_header)
    return
end

-- After connecting, the bitfinex websocket server will send
-- an info message that contains the actual version of the websocket stream.
-- Receive that message..
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

-- We should get this:
-- {"event":"info","version":1.1,"platform":{"status":1}}
print(ws:get_frame_data())

-- Subscribe to the public ticker feed.
-- See https://docs.bitfinex.com/docs for more information.
local json = chilkat.JsonObject.new()
json:append_string("event", "subscribe")
json:append_string("channel", "ticker")
json:append_string("pair", "BTCUSD")

local finalFrame = true
success = ws:send_frame(json:emit(), finalFrame)

-- Read the response.
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

-- Examine the response
-- We should get this:
-- {"event":"subscribed","channel":"ticker","chanId":2751,"pair":"BTCUSD"}
print(ws:get_frame_data())

-- Begin reading the ticker feed.
-- We'll just read the 1st 5 updates and then exit..
local receivedFinalFrame = false
local numUpdatesReceived = 0
while numUpdatesReceived < 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 receivedText = ws:get_frame_data()
        print(receivedText)
        -- Should receive a line of text such as this:
        -- [2751,7349,36.34269559,7349.1,41.01777063,-116.2,-0.0156,7349.1,22188.26055319,7560,7270.5]
        numUpdatesReceived = numUpdatesReceived + 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.")