Tradehub.Stream (Tradehub v0.1.17) View Source
This module enables the power to interact with the Tradehub Demex socket.
Behinds the scene, this module requires two dependencies to communicate with the socket server, and broadcasting the received messages to listeners: WebSockex, and Phoenix PubSub.
To subscribe any topics of the websocket server, you can simply do:
defmodule MyApp.Client do
use Tradehub.Stream, topics: ["market_stats", "recent_trades.swth_eth1"]
def handle_info(message, state) do
# Handle you messages here
IO.puts(message)
{:ok, state}
end
end
Tradehub.Stream
built based on GenServer
so you can easily fit into any supervision tree.
defmodule MyApp.Application do
use Application
def start(_opts, _args) do
children = [
MyApp.Client
]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
Or if you want to manage everything manually, implement your own a process and manualy do subscribe
to any topics you want by using utilize functions of Tradehub.Stream
.
defmodule MyApp.Client do
use GenServer
def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end
## Callbacks
def init(stack) do
# Start listening on the `market_stats` topic
Tradehub.Stream.market_stats()
{:ok, stack}
end
# Handle latest message from the `market_stats` channel
def handle_info(msg, state) do
IO.puts("Receive message -- #{msg}")
{:noreply, state}
end
end
Link to this section Summary
Types
The channel ID
Functions
Subscribes to the account_trades.[account]
channel to request upto 100 trades of the given account.
Subscribes to the account_trades_by_market.[market].[account]
channel to request trades of the given account within a market.
Subscribes to the balances.[account]
channel of the given account to request latest balance updates.
Subscribes to the books.[market]
channel to request the latest orderbook of the given market.
Subscribes to the candlesticks.[market].[resolution]
channel to request latest candlesticks of the market in the resolution timeframe.
Subscribes to the leverages.[account]
channel to request latest leverages information of the given account.
Subscribes to the leverages_by_market.[market].[account]
channel to request latest leverages information of the
given account within a market.
Subscribes to the market_stats
channel to request the latest statistics of the market.
Subscribes to the orders.[account]
channel to request the latest orders of the given account.
Subscribes to the orders_by_market.[market].[account]
channel to request the latest orders of the given account within
a specific market.
Subscribes to the positions.[account]
channel to request the latest positions of the given account.
Subscribes to the positions_by_market.[market].[account]
channel to request the latest positions of the given account
within a particular market
Subscribes to the recent_trades.[market]
channel to request the recent trades of the given market.
The utility function to subscribe into the channel, and broadcast messages if any to the Tradehub.PubSub
with a specific topic name.
The utility function to unsubscribe an available channel
Link to this section Types
Specs
channel_id() :: String.t()
The channel ID
Link to this section Functions
Specs
account_trades(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the account_trades.[account]
channel to request upto 100 trades of the given account.
Examples
iex> topic = Tradehub.Stream.account_trades("swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
account_trades_by_market(String.t(), String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the account_trades_by_market.[market].[account]
channel to request trades of the given account within a market.
Examples
iex> topic = Tradehub.Stream.account_trades_by_market("swth_eth1", "swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
balances(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the balances.[account]
channel of the given account to request latest balance updates.
Examples
iex> topic = Tradehub.Stream.balances("swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
books(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the books.[market]
channel to request the latest orderbook of the given market.
Examples
iex> topic = Tradehub.Stream.books("swth_eth1")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
candlesticks(String.t(), String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the candlesticks.[market].[resolution]
channel to request latest candlesticks of the market in the resolution timeframe.
Examples
iex> topic = Tradehub.Stream.candlesticks("swth_eth1", 30)
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
leverages(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the leverages.[account]
channel to request latest leverages information of the given account.
Examples
iex> topic = Tradehub.Stream.leverages("swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
leverages_by_market(String.t(), String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the leverages_by_market.[market].[account]
channel to request latest leverages information of the
given account within a market.
Examples
iex> topic = Tradehub.Stream.leverages_by_market("swth_eth1", "swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
market_stats() :: channel_id() | {:error, reason :: any()}
Subscribes to the market_stats
channel to request the latest statistics of the market.
Examples
iex> topic = Tradehub.Stream.market_stats
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
orders(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the orders.[account]
channel to request the latest orders of the given account.
Examples
iex> topic = Tradehub.Stream.orders("swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
orders_by_market(String.t(), String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the orders_by_market.[market].[account]
channel to request the latest orders of the given account within
a specific market.
Examples
iex> topic = Tradehub.Stream.orders_by_market("swth_eth1", "swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
positions(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the positions.[account]
channel to request the latest positions of the given account.
Examples
iex> topic = Tradehub.Stream.positions("swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
positions_by_market(String.t(), String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the positions_by_market.[market].[account]
channel to request the latest positions of the given account
within a particular market
Examples
iex> topic = Tradehub.Stream.positions_by_market("swth_eth1", "swth1fdqkq5gc5x8h6a0j9hamc30stlvea6zldprt6q")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
recent_trades(String.t()) :: channel_id() | {:error, reason :: any()}
Subscribes to the recent_trades.[market]
channel to request the recent trades of the given market.
Examples
iex> topic = Tradehub.Stream.recent_trades("swth_eth1")
iex> Process.info(self(), :messages)
iex> Tradehub.Stream.unsubscribe topic
Specs
subscribe(String.t()) :: channel_id() | {:error, reason :: any()}
The utility function to subscribe into the channel, and broadcast messages if any to the Tradehub.PubSub
with a specific topic name.
Examples
iex> Tradehub.Stream.subscribe("market_stats")
Specs
The utility function to unsubscribe an available channel
Examples
iex> Tradehub.Stream.unsubscribe("market_stats")