Skip to content

Connection goes silent without errors (Binance) #96

@rm-rf-etc

Description

@rm-rf-etc

Erlang version: 21.3
Elixir: 1.11.4
WebSockex: 0.4.2
Computer: MacBook Air (M1, 2020)
OS: Big Sur v11.0.1

Subscribing to the WS feed from Binance results in a successful connection and the first 2 - 3 messages come in as expected, however, the connection then goes silent. I don't see any indication of the connection closing. I've enabled tracing using debug: [:trace], but this also doesn't yield any clues (that I can see). Included is a minimal reproduction of the bug:

defmodule Example do
  use WebSockex

  @url "wss://stream.binance.com:9443/ws/ethusdt"

  @subscribe_msg ~s({"id":1,"method":"SUBSCRIBE","params":["ethusdt@kline_1m"]})

  def start_link() do
    {:ok, pid} = WebSockex.start_link(@url, __MODULE__, :fake_state)
    WebSockex.send_frame(pid, {:text, @subscribe_msg})

    {:ok, pid}
  end

  def handle_frame({type, msg}, state) do
    IO.puts("Received Message - Type: #{inspect(type)} -- Message: #{inspect(msg)}")
    {:ok, state}
  end

  def handle_cast({:send, {type, msg} = frame}, state) do
    IO.puts("Sending #{type} frame with payload: #{msg}")
    {:reply, frame, state}
  end
end

I don't think the server is the cause of the problem, because it seems to work as expected when I test with wscat:

wscat -c 'wss://stream.binance.com:9443/ws/ethusdt'
Connected (press CTRL+C to quit)
> {"id":1,"method":"SUBSCRIBE","params":["ethusdt@kline_1m"]}
< {"result":null,"id":1}
< {"e":"kline","E":1620887236596,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829531,"o":"3996.07000000","c":"3989.84000000","h":"3996.11000000","l":"3988.82000000","v":"105.39922000","n":348,"x":false,"q":"420728.46134770","V":"31.94498000","Q":"127512.32144190","B":"0"}}
< {"e":"kline","E":1620887238601,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829585,"o":"3996.07000000","c":"3989.83000000","h":"3996.11000000","l":"3988.82000000","v":"114.74677000","n":402,"x":false,"q":"458023.10700630","V":"39.87059000","Q":"159134.03102530","B":"0"}}
< {"e":"kline","E":1620887240673,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829631,"o":"3996.07000000","c":"3989.62000000","h":"3996.11000000","l":"3988.82000000","v":"131.52760000","n":448,"x":false,"q":"524975.86242910","V":"50.03652000","Q":"199694.45886110","B":"0"}}
< {"e":"kline","E":1620887242751,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829645,"o":"3996.07000000","c":"3989.65000000","h":"3996.11000000","l":"3988.82000000","v":"132.11121000","n":462,"x":false,"q":"527304.13949430","V":"50.12365000","Q":"200042.07585250","B":"0"}}
< {"e":"kline","E":1620887244861,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829679,"o":"3996.07000000","c":"3988.34000000","h":"3996.11000000","l":"3988.34000000","v":"143.28122000","n":496,"x":false,"q":"571863.00172220","V":"58.58588000","Q":"233800.39506240","B":"0"}}
< {"e":"kline","E":1620887247017,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829698,"o":"3996.07000000","c":"3988.34000000","h":"3996.11000000","l":"3988.34000000","v":"151.36197000","n":515,"x":false,"q":"604091.85678690","V":"66.24685000","Q":"264355.02476190","B":"0"}}
< {"e":"kline","E":1620887249037,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829739,"o":"3996.07000000","c":"3986.62000000","h":"3996.11000000","l":"3986.62000000","v":"161.63306000","n":556,"x":false,"q":"645045.04841330","V":"73.11274000","Q":"291728.58779110","B":"0"}}

...keeps going...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions