Sample code for 30+ languages & platforms
Rust

Read Bitfinex WebSocket Ticker Channel

See more WebSocket Examples

Subscribes to the public Bitfinex websocket ticker channel and receives ticker updates.

Chilkat Rust Downloads

Rust

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

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

let rest = chilkat::Rest::new();

// Connect to api.bitfinex.com
let _ = rest.connect("api.bitfinex.com", 443, true, false).is_ok();
let _ = ws.use_connection(&rest);
let _ = ws.add_client_headers();

let response_body = rest.full_request_no_body("GET", "/ws").unwrap_or_default();
if ws.validate_server_handshake().is_err() {
    println!("{}", ws.last_error_text());
    println!("{}", response_body);
    println!("{}", rest.response_header());
    return;
}

// After connecting, the bitfinex websocket server will send
// an info message that contains the actual version of the websocket stream.
// Receive that message..
if ws.read_frame().is_err() {
    println!("Failed to receive a frame");
    println!("ReadFrame fail reason = {}", ws.read_frame_fail_reason());
    println!("{}", ws.last_error_text());
    return;
}

// We should get this:
// {"event":"info","version":1.1,"platform":{"status":1}}
println!("{}", ws.get_frame_data().unwrap_or_default());

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

let final_frame = true;
let _ = ws.send_frame(&json.emit().unwrap_or_default(), final_frame).is_ok();

// Read the response.
if ws.read_frame().is_err() {
    println!("Failed to receive a frame");
    println!("ReadFrame fail reason = {}", ws.read_frame_fail_reason());
    println!("{}", ws.last_error_text());
    return;
}

// Examine the response
// We should get this:
// {"event":"subscribed","channel":"ticker","chanId":2751,"pair":"BTCUSD"}
println!("{}", ws.get_frame_data().unwrap_or_default());

// Begin reading the ticker feed.
// We'll just read the 1st 5 updates and then exit..
let mut num_updates_received = 0;
while num_updates_received < 5 {

    if ws.read_frame().is_err() {
        println!("Failed to receive a frame");
        println!("ReadFrame fail reason = {}", ws.read_frame_fail_reason());
        println!("{}", ws.last_error_text());
        return;
    }

    // The responses we desire are in Text frames, where the opcode = 1.
    if ws.frame_opcode_int() == 1 {
        let received_text = ws.get_frame_data().unwrap_or_default();
        println!("{}", received_text);
        // 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]
        num_updates_received = num_updates_received + 1;
    }

}

// Close the websocket connection.
if ws.send_close(true, 1000, "Closing this websocket.").is_err() {
    println!("{}", ws.last_error_text());
    return;
}

// Read the Close response.
if ws.read_frame().is_err() {
    println!("ReadFrame fail reason = {}", ws.read_frame_fail_reason());
    println!("{}", ws.last_error_text());
    return;
}

println!("Success.");