From 892c2722339818dc560b728d8d491fdad8858e2f Mon Sep 17 00:00:00 2001 From: jrusso1020 Date: Sat, 25 Jul 2020 16:25:42 -0400 Subject: [PATCH] Add support for multiple streams With these small modifications you should be able to use the `Alpaca.Stream` module to implement multiple streams and pass in an optional `url` keyword for different web socket streams in the Alpaca API like the data stream as well as the trading stream. --- lib/alpaca/stream.ex | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/alpaca/stream.ex b/lib/alpaca/stream.ex index 662d492..11b9005 100644 --- a/lib/alpaca/stream.ex +++ b/lib/alpaca/stream.ex @@ -5,7 +5,7 @@ defmodule Alpaca.Stream do A simple client implementation would be: ``` defmodule AlpacaStreamClient do - use Alpaca.Stream + use Alpaca.Stream, url: "https://paper-api.alpaca.markets/stream" def start_link() do start_link(["account_updates", "trade_updates"]) @@ -18,6 +18,10 @@ defmodule Alpaca.Stream do end end ``` + + The `url` keyword is optional and if omitted will be defaulted to `"#{Client.api_host()}/stream"` + to key backwards compatibility. + ## Supervision Alpaca.Stream uses WebSockex under the hood @@ -41,7 +45,7 @@ defmodule Alpaca.Stream do ### Example ``` defmodule TestStream do - use Alpaca.Stream + use Alpaca.Stream, url: "wss://data.alpaca.markets/stream" @impl Alpaca.Stream def handle_msg(msg, state) do @@ -58,7 +62,9 @@ defmodule Alpaca.Stream do | {:close, WebSockex.close_frame(), new_state} when new_state: term - defmacro __using__([]) do + defmacro __using__(opts \\ []) do + url = Keyword.get(opts, :url, "#{Alpaca.Client.api_host()}/stream") + quote do use WebSockex @@ -88,7 +94,7 @@ defmodule Alpaca.Stream do ``` """ def start_link(streams) do - {:ok, pid} = WebSockex.start_link("#{Client.api_host()}/stream", __MODULE__, :no_state) + {:ok, pid} = WebSockex.start_link(unquote(url), __MODULE__, :no_state) authenticate(pid) unless streams == [] do