In the previous article, we’ve integrated Elixir Phoenix with Binance WebSocket streams. In this part, we talk about how to work with Coinbase WebSocket streams.
For Coinbase WebSocket streams there is a Coinbase Pro REST and WebSocket client called GDEX for Elixir. It makes this job easier. So, firstly we need to add GDEX into the project and run “mix deps.get”
Secondly, Define a module that uses the Gdex.Websocket
behavior:
According to the Coinbase WebSocket channels doc, the “ticker_batch” channel meets our requirements, so once the WebSocket is connected, it will subscribe “ticker_batch” channel.
def handle_connect(gdax, state) do
Logger.debug("Coinbase websocket Connected!")
symbols = Enum.reduce(XSureWss.TickerUtils.get_crypto_symbols(), [], fn (symbol, acc) ->
acc ++ ["#{symbol}-USD"]
end)
subscribe(gdax, :ticker_batch, symbols, authenticate: true)
{:ok, state}
end
Finally, start the WebSocket client with the handler and append XSureWss.Coinbase.Streamer to Phoenix application start:
That is! Run mix phx.server
. If everything goes well, you will see the following stream logs.
Source Code
You can find the complete finished code in this GitHub repository.