sad
sad

Reputation: 820

Erlang websocket client with cowboy server

Im trying to use this websocket client library but with little success. Erlang websocket client

If someone used this library to build a client talking to a remote server, how were you able to send messages ?

The basic usage shows to call this to inititate a connection,

websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).

and cast/2 to send a message to a remote server.

websocket_client:cast(self(), {text, <<"message 1">>}).

However, if I try to use the same function else where in the code to send a text/binary frame to remote server, its not helping.

Is there anything that Im missing ?

Thanks!

Upvotes: 1

Views: 685

Answers (1)

johlo
johlo

Reputation: 5500

Keep in mind that the first parameter to websocket_client:cast/2 must be the pid for the websocket_client process. You can get the pid from the start_link call, e.g.:

{ok, Pid} = websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).

And to cast a message to the remote server:

websocket_client:cast(Pid, {text, <<"message 1">>}).

In the example code for the websocket_client project cast is called from within the init function, in this case they can use self() since the init function is actually executed by the websocket client process.

Similarly if you are calling cast from within your websocket_handle/websocket_info callback functions you can use self() since those are also called by the websocket client process.

Upvotes: 2

Related Questions