{"id":15063599,"url":"https://github.com/arturogonzalezm/binance_websocket_client","last_synced_at":"2026-01-01T21:58:37.895Z","repository":{"id":251499456,"uuid":"837593968","full_name":"arturogonzalezm/binance_websocket_client","owner":"arturogonzalezm","description":"This project connects to Binance's WebSocket API to process real-time cryptocurrency data using the observer pattern.","archived":false,"fork":false,"pushed_at":"2025-11-04T18:53:17.000Z","size":46,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-04T19:14:20.898Z","etag":null,"topics":["binance-api","elixir","elixir-lang","elixir-programming-language","phoenix","phoenix-channels","real-time","websockets"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arturogonzalezm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-03T12:39:12.000Z","updated_at":"2025-11-04T18:56:26.000Z","dependencies_parsed_at":"2024-09-29T09:01:30.034Z","dependency_job_id":"9911b334-cdc9-4e51-bfa4-8d99f130f5ec","html_url":"https://github.com/arturogonzalezm/binance_websocket_client","commit_stats":null,"previous_names":["arturogonzalezm/binance_websocket_client"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arturogonzalezm/binance_websocket_client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturogonzalezm%2Fbinance_websocket_client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturogonzalezm%2Fbinance_websocket_client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturogonzalezm%2Fbinance_websocket_client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturogonzalezm%2Fbinance_websocket_client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arturogonzalezm","download_url":"https://codeload.github.com/arturogonzalezm/binance_websocket_client/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturogonzalezm%2Fbinance_websocket_client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28164122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2026-01-01T02:00:06.694Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binance-api","elixir","elixir-lang","elixir-programming-language","phoenix","phoenix-channels","real-time","websockets"],"created_at":"2024-09-25T00:04:47.162Z","updated_at":"2026-01-01T21:58:37.889Z","avatar_url":"https://github.com/arturogonzalezm.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binance WebSocket Client\n\nThis project implements a WebSocket client for the Binance cryptocurrency exchange, specifically for receiving real-time ticker updates for the BTCUSDT trading pair.\n\n## Features\n\n- Real-time WebSocket connection to Binance\n- Ticker data storage and retrieval\n- Subscription system for receiving updates\n- Configurable filtering of updates\n\n## Prerequisites\n\n- Elixir 1.19 or later\n- Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 16.1\n- Erlang/OTP 28\n- IntelliJ IDEA Version 2025.1.6 with Elixir plugin (optional)\n- Homebrew\n- asdf for Elixir version management\n\n## Installation\n\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/arturogonzalezm/binance_websocket_client.git\n   cd binance_websocket_client\n   ```\n\n2. Fetch dependencies:\n   ```bash\n   mix deps.get\n   ```\n\n3. Compile the project:\n   ```bash\n   mix compile\n   ```\n\n4. Test:\n   ```bash\n   mix test\n   ```\n\n## Usage\n\nTo start the application:\n\n```bash\niex -S mix\n```\n\n```elixir\nApplication.stop(:binance_websocket_client)\n```\n\n```elixir\nApplication.start(:binance_websocket_client)\n```\n\nThis will start the WebSocket client and connect to Binance's WebSocket server.\n\n### Getting the latest BTCUSDT ticker\n\nAfter starting the app, at any time you can fetch the most recent ticker payload the client received from Binance:\n\n```elixir\nBinanceWebsocketClient.latest()\n# =\u003e %{\"e\" =\u003e \"24hrTicker\", \"s\" =\u003e \"BTCUSDT\", ...}\n```\n\n### Creating a Subscriber\n\nTo receive real-time updates as they arrive:\n\n```elixir\nhandler = fn ticker -\u003e IO.inspect(ticker, label: \"Received ticker update\") end\n{:ok, pid} = BinanceWebsocketClient.subscribe(handler)\n```\n\nTo filter updates (for example, only when 24h change percent \u003e 1%):\n\n```elixir\nfilter = fn ticker -\u003e String.to_float(ticker[\"P\"]) \u003e 1.0 end\n{:ok, pid} = BinanceWebsocketClient.subscribe(handler, filter: filter)\n```\n\n## Architecture\n\n### Flowchart\n\n```mermaid\ngraph TD\n    A[Binance WebSocket Server] --\u003e|WebSocket Connection| B(BinanceWebsocketClient)\n    B --\u003e|Updates| C{TickerStore}\n    C --\u003e|Notify| D[Subscriber 1]\n    C --\u003e|Notify| E[Subscriber 2]\n    C --\u003e|Notify| F[Subscriber N]\n```\n\n### Sequence Diagram\n\n```mermaid\nsequenceDiagram\n    participant Binance as Binance WebSocket Server\n    participant Client as BinanceWebsocketClient\n    participant Store as TickerStore\n    participant Sub as Subscriber\n\n    Sub-\u003e\u003eStore: subscribe()\n    Binance-\u003e\u003eClient: Ticker Update\n    Client-\u003e\u003eStore: update(ticker_data)\n    Store-\u003e\u003eSub: notify(ticker_data)\n    Sub-\u003e\u003eSub: handle(ticker_data)\n```\n\n## Running Tests\n\nTo run the test suite:\n\n```\nmix test\n```\n\n## TODO: \n- Dockerise the Application\n- Add docker-compose file\n- Add CI/CD pipeline\n- Add more tests\n- Add InfluxDB for storing ticker data\n- Add Grafana for visualising ticker data\n- Add Prometheus for monitoring the application\n- Add Kubernetes for deploying the application\n- Add Helm for managing the Kubernetes deployment\n- Add Terraform for managing the infrastructure\n- Add Ansible for managing the configuration\n- Add Jenkins for automating the CI/CD pipeline\n- Add SonarQube for code quality analysis\n- Add ELK Stack for log management\n- Add Jaeger for distributed tracing\n- Add OpenTelemetry for observability\n- Add Sentry for error tracking\n- Add New Relic for application performance monitoring\n- Add Datadog for infrastructure monitoring\n- Build a front-end for visualising the data in real-time using Phoenix LiveView\n\n## Phoenix Channels Frontend\n\nA minimal Phoenix Endpoint with Channels is included so you can consume real-time BTCUSDT ticker updates over WebSockets.\n\n- Endpoint: ws://localhost:4000/socket\n- Channel: \"ticker:btcusdt\"\n- Events:\n  - latest: pushed once on join with the current ticker if available\n  - update: pushed on every new ticker update from Binance\n\n### How to run the endpoint\n\n1. Fetch new dependencies and compile:\n   ```bash\n   mix deps.get\n   mix compile\n   ```\n2. Start the application (this also starts the Phoenix endpoint):\n   ```bash\n   iex -S mix\n   ```\n\nYou should now have a WebSocket server running at ws://localhost:4000/socket.\n\n### JavaScript usage (browser)\n\nYou can use Phoenix's JavaScript client to connect and receive updates. Example using a CDN:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"utf-8\" /\u003e\n    \u003ctitle\u003eBTCUSDT Ticker\u003c/title\u003e\n    \u003cscript src=\"https://unpkg.com/phoenix@1.7.14/priv/static/phoenix.min.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cpre id=\"log\"\u003e\u003c/pre\u003e\n    \u003cscript\u003e\n      const log = (msg, data) =\u003e {\n        const el = document.getElementById(\"log\");\n        el.textContent += `\\n${msg} ${data ? JSON.stringify(data) : ''}`;\n      };\n\n      const socket = new window.Phoenix.Socket(\"ws://localhost:4000/socket\", { params: {} });\n      socket.connect();\n\n      const channel = socket.channel(\"ticker:btcusdt\", {});\n\n      channel.on(\"latest\", (payload) =\u003e log(\"latest:\", payload));\n      channel.on(\"update\", (payload) =\u003e log(\"update:\", payload));\n\n      channel.join()\n        .receive(\"ok\", () =\u003e log(\"joined ticker:btcusdt\"))\n        .receive(\"error\", resp =\u003e log(\"join error:\", resp));\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Elixir usage (via Phoenix.ChannelTest / clients)\n\nFrom Elixir, you can also write a small client using Phoenix Channels protocol libraries, but for most frontend uses, the JS client above is recommended.\n\nNotes:\n- The WebSocket server only exposes the channel and does not serve HTML pages.\n- The topic is currently fixed to BTCUSDT. If you expand to multiple pairs, broadcast to corresponding topics like \"ticker:ethusdt\".\n\n## Web Dashboard (Phoenix)\n\nAn elegant, minimal dashboard is included and served by the built‑in Phoenix endpoint.\n\n- URL: http://localhost:4000/\n- Live data via: ws://localhost:4000/socket (Phoenix Channels)\n- Topic: \"ticker:btcusdt\"\n- Features:\n  - Real‑time price line chart (Chart.js)\n  - Key KPIs: Price, 24h change %, high/low, base and quote volume\n  - Live feed of the last 50 ticks\n  - Pause/resume and Clear controls\n\nHow to run:\n1. mix deps.get \u0026\u0026 mix compile\n2. iex -S mix\n3. Open http://localhost:4000/ in your browser.\n\nNotes:\n- The endpoint serves static assets from priv/static (index.html, styles.css, app.js).\n- If you don’t see data, confirm the console shows the connection status as \"Live\" and that your network allows outbound WebSocket connections.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.\n\n## Connection errors (HTTP 451) and configuration\n\nSome regions block access to Binance's global WebSocket endpoint and you may see an error like:\n\n```\n** (EXIT) %WebSockex.RequestError{code: 451, message: \"\"}\n```\n\nThe application now starts even if the Binance WebSocket is temporarily unreachable or blocked. It will retry the connection every 15 seconds in the background.\n\nIf you are in a restricted region, point the client to an alternative endpoint (for example, binance.us):\n\n```bash\n# Use binance.us 24h ticker for BTCUSDT\nexport BINANCE_WS_URL=\"wss://stream.binance.us:9443/ws/btcusdt@ticker\"\n# then start the app\niex -S mix\n```\n\nYou can also set the URL via config (default is the global binance.com endpoint):\n\n```elixir\n# config/config.exs\nconfig :binance_websocket_client, :ws_url, \"wss://stream.binance.com:9443/ws/btcusdt@ticker\"\n```\n\nPrecedence for the WebSocket URL:\n- opts[:url] when starting the client programmatically\n- BINANCE_WS_URL environment variable\n- config :binance_websocket_client, :ws_url (default)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturogonzalezm%2Fbinance_websocket_client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farturogonzalezm%2Fbinance_websocket_client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturogonzalezm%2Fbinance_websocket_client/lists"}