Sample code for 30+ languages & platforms
C

WebSocket Binance Trade Stream (subscribe and receive updates)

See more WebSocket Examples

Subscribe to a binance trade stream and receive updates.

Chilkat C Downloads

C
#include <C_CkWebSocket.h>
#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkWebSocket ws;
    HCkRest rest;
    const char *responseBody;
    HCkJsonObject json;
    BOOL finalFrame;
    HCkJsonObject jsonTradeData;
    BOOL receivedFinalFrame;
    int numTradesReceived;
    const char *receivedJson;

    success = FALSE;

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

    ws = CkWebSocket_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 = CkRest_Create();

    // Connect to wss://stream.binance.com:9443
    success = CkRest_Connect(rest,"stream.binance.com",9443,TRUE,FALSE);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        return;
    }

    success = CkWebSocket_UseConnection(ws,rest);
    if (success == FALSE) {
        printf("%s\n",CkWebSocket_lastErrorText(ws));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        return;
    }

    CkWebSocket_AddClientHeaders(ws);

    //  Raw streams are accessed at /ws/<streamName>
    responseBody = CkRest_fullRequestNoBody(rest,"GET","/ws/btcusdt");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        return;
    }

    success = CkWebSocket_ValidateServerHandshake(ws);
    if (success != TRUE) {
        printf("%s\n",CkWebSocket_lastErrorText(ws));
        printf("%s\n",responseBody);
        printf("%s\n",CkRest_responseHeader(rest));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        return;
    }

    printf("%s\n",responseBody);
    printf("%s\n",CkRest_responseHeader(rest));

    // POST JSON to subscribe to a stream

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

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"method","SUBSCRIBE");
    CkJsonObject_UpdateString(json,"params[0]","btcusdt@aggTrade");
    CkJsonObject_UpdateString(json,"params[1]","btcusdt@depth");
    CkJsonObject_UpdateInt(json,"id",1);

    // Send a full message in a single frame
    finalFrame = TRUE;
    success = CkWebSocket_SendFrame(ws,CkJsonObject_emit(json),finalFrame);
    if (success != TRUE) {
        printf("%s\n",CkWebSocket_lastErrorText(ws));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        return;
    }

    jsonTradeData = CkJsonObject_Create();
    CkJsonObject_putEmitCompact(jsonTradeData,FALSE);

    // Begin reading the trade stream response.
    // We'll just read the 1st 10 updates and then exit..
    receivedFinalFrame = FALSE;
    numTradesReceived = 0;
    while (numTradesReceived < 5) {

        success = CkWebSocket_ReadFrame(ws);
        if (success != TRUE) {
            printf("Failed to receive a frame\n");
            printf("ReadFrame fail reason = %d\n",CkWebSocket_getReadFrameFailReason(ws));
            printf("%s\n",CkWebSocket_lastErrorText(ws));
            CkWebSocket_Dispose(ws);
            CkRest_Dispose(rest);
            CkJsonObject_Dispose(json);
            CkJsonObject_Dispose(jsonTradeData);
            return;
        }

        // The responses we desire are in Text frames, where the opcode = 1.
        if (CkWebSocket_getFrameOpcodeInt(ws) == 1) {
            receivedJson = CkWebSocket_getFrameData(ws);

            CkJsonObject_Load(jsonTradeData,receivedJson);
            printf("%s\n",CkJsonObject_emit(jsonTradeData));

            numTradesReceived = numTradesReceived + 1;
        }

    }

    // Close the websocket connection.
    success = CkWebSocket_SendClose(ws,TRUE,1000,"Closing this websocket.");
    if (success != TRUE) {
        printf("%s\n",CkWebSocket_lastErrorText(ws));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonTradeData);
        return;
    }

    // Read the Close response.
    success = CkWebSocket_ReadFrame(ws);
    if (success != TRUE) {
        printf("ReadFrame fail reason = %d\n",CkWebSocket_getReadFrameFailReason(ws));
        printf("%s\n",CkWebSocket_lastErrorText(ws));
        CkWebSocket_Dispose(ws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonTradeData);
        return;
    }

    printf("Success.\n");

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


    CkWebSocket_Dispose(ws);
    CkRest_Dispose(rest);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonTradeData);

    }