Sample code for 30+ languages & platforms
Unicode C

Read Bitfinex WebSocket Ticker Channel

See more WebSocket Examples

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkWebSocketW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkWebSocketW ws;
    HCkRestW rest;
    const wchar_t *responseBody;
    HCkJsonObjectW json;
    BOOL finalFrame;
    BOOL receivedFinalFrame;
    int numUpdatesReceived;
    const wchar_t *receivedText;

    success = FALSE;

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

    ws = CkWebSocketW_Create();

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

    rest = CkRestW_Create();

    // Connect to api.bitfinex.com
    success = CkRestW_Connect(rest,L"api.bitfinex.com",443,TRUE,FALSE);
    CkWebSocketW_UseConnection(ws,rest);
    CkWebSocketW_AddClientHeaders(ws);

    responseBody = CkRestW_fullRequestNoBody(rest,L"GET",L"/ws");
    success = CkWebSocketW_ValidateServerHandshake(ws);
    if (success != TRUE) {
        wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
        wprintf(L"%s\n",responseBody);
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        CkWebSocketW_Dispose(ws);
        CkRestW_Dispose(rest);
        return;
    }

    // After connecting, the bitfinex websocket server will send
    // an info message that contains the actual version of the websocket stream.
    // Receive that message..
    success = CkWebSocketW_ReadFrame(ws);
    if (success != TRUE) {
        wprintf(L"Failed to receive a frame\n");
        wprintf(L"ReadFrame fail reason = %d\n",CkWebSocketW_getReadFrameFailReason(ws));
        wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
        CkWebSocketW_Dispose(ws);
        CkRestW_Dispose(rest);
        return;
    }

    // We should get this:
    // {"event":"info","version":1.1,"platform":{"status":1}}
    wprintf(L"%s\n",CkWebSocketW_getFrameData(ws));

    // Subscribe to the public ticker feed.
    // See https://docs.bitfinex.com/docs for more information.
    json = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(json,L"event",L"subscribe");
    CkJsonObjectW_AppendString(json,L"channel",L"ticker");
    CkJsonObjectW_AppendString(json,L"pair",L"BTCUSD");

    finalFrame = TRUE;
    success = CkWebSocketW_SendFrame(ws,CkJsonObjectW_emit(json),finalFrame);

    // Read the response.
    success = CkWebSocketW_ReadFrame(ws);
    if (success != TRUE) {
        wprintf(L"Failed to receive a frame\n");
        wprintf(L"ReadFrame fail reason = %d\n",CkWebSocketW_getReadFrameFailReason(ws));
        wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
        CkWebSocketW_Dispose(ws);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Examine the response
    // We should get this:
    // {"event":"subscribed","channel":"ticker","chanId":2751,"pair":"BTCUSD"}
    wprintf(L"%s\n",CkWebSocketW_getFrameData(ws));

    // Begin reading the ticker feed.
    // We'll just read the 1st 5 updates and then exit..
    receivedFinalFrame = FALSE;
    numUpdatesReceived = 0;
    while (numUpdatesReceived < 5) {

        success = CkWebSocketW_ReadFrame(ws);
        if (success != TRUE) {
            wprintf(L"Failed to receive a frame\n");
            wprintf(L"ReadFrame fail reason = %d\n",CkWebSocketW_getReadFrameFailReason(ws));
            wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
            CkWebSocketW_Dispose(ws);
            CkRestW_Dispose(rest);
            CkJsonObjectW_Dispose(json);
            return;
        }

        // The responses we desire are in Text frames, where the opcode = 1.
        if (CkWebSocketW_getFrameOpcodeInt(ws) == 1) {
            receivedText = CkWebSocketW_getFrameData(ws);
            wprintf(L"%s\n",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;
        }

    }

    // Close the websocket connection.
    success = CkWebSocketW_SendClose(ws,TRUE,1000,L"Closing this websocket.");
    if (success != TRUE) {
        wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
        CkWebSocketW_Dispose(ws);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Read the Close response.
    success = CkWebSocketW_ReadFrame(ws);
    if (success != TRUE) {
        wprintf(L"ReadFrame fail reason = %d\n",CkWebSocketW_getReadFrameFailReason(ws));
        wprintf(L"%s\n",CkWebSocketW_lastErrorText(ws));
        CkWebSocketW_Dispose(ws);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"Success.\n");


    CkWebSocketW_Dispose(ws);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(json);

    }