Reputation: 19456
I'm a newbie to Elixir/Erlang. By way of example, I'm trying to learn how to subscribe to a websocket feed.
I've copied the Websockex basic example:
defmodule WebSocketExample do
use WebSockex
def start_link(url, state) do
WebSockex.start_link(url, __MODULE__, state)
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
and am trying to instantiate it in iex with:
WebSocketExample.start_link "wss://www.bitmex.com/realtime?subscribe=instrument,quote:XBTUSD", state
(CompileError) iex:3: undefined function state/0
however I get the error that state is undefined.
I'm not sure what state should be (surely that's empty and gets passed around a loop?). Any tips that help me understand would be much appreciated!
Upvotes: 1
Views: 572
Reputation: 222080
state
here can be any value. You can use it to store information about the socket instance which can later be accessed in the handle functions. For example, you can pass a user id to start_link
and access that from handle_cast
and handle_info
. If you don't want to keep any state, just pass any value, e.g. :ok
:
WebSocketExample.start_link "wss://...", :ok
Upvotes: 2