Hi AD,<br><br>I also tried to implement a websocket broadcast as you did
in this mail. but I got crash in websocket_info as following. Could
you please show me your complete code? thanks!<br><br>my code is:<br><br>websocket_init(_Any, Req, []) -><br>
gproc:reg({p, l,{?MODULE, ?BC}}),<br> Req2 = cowboy_http_req:compact(Req),<br> {ok, Req2, undefined, hibernate}.<br><br>websocket_handle({text, Msg}, Req, State) -><br> broadcast(Msg),<br> {reply, {text, << "You said: ", Msg/binary >>}, Req, State, hibernate};<br>
websocket_handle(_Any, Req, State) -><br> {ok, Req, State}.<br><br>websocket_info({_Pid, {_Module, ?BC}, Msg}, Req, State) -><div class="im"><br> {reply, {text, <<Msg>>}, Req, State, hibernate}.<br>
<br></div>broadcast(Msg) -><br>
gproc:send({p, l, {?MODULE, ?BC}}, {self(), {?MODULE, ?BC}, Msg}).<br><br><br><br>** Handler websocket_handler terminating in websocket_info/3<div class="im"><br> for the reason error:badarg<br></div>** Message was {<0.113.0>,{websocket_handler,<div id=":h2">
"broadcast"},<<"hello server!">>}<br>
** Options were []<br>** Handler state was undefined<br>** Request was [{socket,#Port<0.940>},<br> {transport,cowboy_tcp_transport},<br> {connection,keepalive},<br> {pid,<0.113.0>},<br>
{method,'GET'},<br> {version,{1,1}},<br> {peer,undefined},<br> {host,undefined},<br> {host_info,undefined},<br> {raw_host,<<"localhost">>},<br>
{port,80},<br> {path,undefined},<br> {path_info,undefined},<br> {raw_path,<<"/websocket">>},<br> {qs_vals,undefined},<br> {raw_qs,<<>>},<br>
{bindings,undefined},<br> {headers,[]},<br> {p_headers,[]},<br> {cookies,[]},<br> {meta,[{websocket_version,13}]},<br> {body_state,waiting},<br>
{buffer,<<>>},<br> {resp_state,done},<br> {resp_headers,[]},<br> {resp_body,<<>>},<br> {urldecode,{#Fun<cowboy_http.urldecode.2>,crash}}]<br>
** Stacktrace: [{websocket_handler,websocket_info,3,<br> [{file,"src/websocket_handler.erl"},{line,39}]},<br> {cowboy_http_websocket,handler_call,7,<br> [{file,"src/cowboy_http_websocket.erl"},{line,389}]}]</div>
<br><br><div class="gmail_quote">On Sat, Mar 17, 2012 at 4:56 AM, AD <span dir="ltr"><<a href="mailto:straightflush@gmail.com">straightflush@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Sigh, no. Thanks, its working perfectly now.<span class="HOEnZb"><font color="#888888"><div><br></div></font></span><div><span class="HOEnZb"><font color="#888888">-AD</font></span><div><div class="h5"><br><br><div class="gmail_quote">
On Fri, Mar 16, 2012 at 4:18 PM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Did you start gproc? application:start(gproc)<div><br>
<br>
On 03/16/2012 09:05 PM, AD wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Thanks, getting some stacktraces though...<br>
<br>
** Handler ad_test terminating in websocket_init/3<br>
for the reason error:badarg<br>
<br>
i have<br>
<br></div>
/-define(WSBroadcast,"<u></u>wsbroadcast")./<br>
<br>
then in websocket_init<br>
<br>
/gproc:reg({p, l, {?MODULE, ?WSBroadcast}})/<br>
/<div><br>
/<br>
then in my handle() method in cowboy for a certain path<br>
<br></div>
/{URLPath,_Req2} = cowboy_http_req:path(Req),/<br>
/ case lists:nth(1,URLPath) of/<br>
/<<"sendit">> -> /<br>
/%io:format("Sending websocket !!~n"),/<br>
/Msg = "Test broadcast",/<br>
/gproc:send({p, l, {?MODULE,?WSBroadcast}}, {self(),<br>
{?MODULE,?WSBroadcast}, Msg});/<br>
/<br>
/<br>
then in websocket_info<br>
<br>
w/ebsocket_info({_PID,{_<u></u>MODULE,_WSBroadcast},Msg},Req,<u></u>State) ->/<br>
/{reply, {text, <<Msg>>}, Req, State, hibernate}./<div><br>
<br>
Any thoughts ? Looks like something with ets:insert<br>
<br>
** Stacktrace: [{ets,insert_new,<br>
[gproc,<br>
[{{{p,l,{ad_test,"wsbroadcast"<u></u>}},<0.153.0>},<br>
<0.153.0>,undefined},<br>
{{<0.153.0>,{p,l,{ad_test,"<u></u>wsbroadcast"}}},[]}]],<br>
[]},<br>
<br>
Thanks again for the help<br>
-AD<br>
<br>
On Fri, Mar 16, 2012 at 2:06 PM, Loïc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br></div><div>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>> wrote:<br>
<br>
Just like you would catch any message, you match:<br>
<br>
websocket_info({Pid, {Module, WSBroadcast}, Msg}, Req, State)<br>
<br>
When using gproc:send/2, the second arg is what you want to match in<br>
the first arg of websocket_info/3. All messages the websocket<br>
process receives are given to you in websocket_info/3.<br>
<br>
Didn't try the new pubsub, it looks interesting though.<br>
<br>
<br>
On 03/16/2012 07:01 PM, AD wrote:<br>
<br>
Thanks, so in websocket/init<br>
<br>
gproc:reg({p, l, {?MODULE, WSBroadcast}}).<br>
<br>
Then in my webservice i can do<br>
<br>
Msg = "Test broadcast".<br>
gproc:send({p, l, {?MODULE,WSBroadcast}}, {self(),<br>
{?MODULE,WSBroadcast}, Msg}).<br>
<br>
How do you catch this in websocket_info/3 to formulate a reply?<br>
<br>
Also noticed a new pub/sub module for gproc not sure if this helps<br>
simplify some of this<br></div>
<a href="https://github.com/uwiger/__gproc/blob/master/src/gproc___ps.erl" target="_blank">https://github.com/uwiger/__<u></u>gproc/blob/master/src/gproc___<u></u>ps.erl</a><div><br>
<<a href="https://github.com/uwiger/gproc/blob/master/src/gproc_ps.erl" target="_blank">https://github.com/uwiger/<u></u>gproc/blob/master/src/gproc_<u></u>ps.erl</a>><br>
<br>
Thanks!<br>
-AD<br>
<br>
On Fri, Mar 16, 2012 at 12:20 PM, Loïc Hoguin<br>
<<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>><br></div><div>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>>> wrote:<br>
<br>
Hey!<br>
<br>
The general idea is to have your websocket handler register<br>
itself<br>
using gproc[1] (or through a central gen_server) inside the<br>
websocket_init/3 callback, and then have your service push<br>
messages<br>
to all registered handlers which you can then catch in<br>
websocket_info/3.<br>
<br>
I mention gproc because it's infinitely easier with it, as<br>
you only<br>
have to register all your processes under one property and<br>
then send<br>
a message to that property which will multicast it to all<br>
registered<br>
processes. And when your websocket closes, gproc takes care of<br>
removing your process from the list of registered processes,<br>
so you<br>
really have to worry only about 2 lines of code to do<br>
everything you<br>
need.<br>
<br>
Good luck!<br>
<br></div>
[1] <a href="https://github.com/uwiger/____gproc" target="_blank">https://github.com/uwiger/____<u></u>gproc</a><br>
<<a href="https://github.com/uwiger/__gproc" target="_blank">https://github.com/uwiger/__<u></u>gproc</a>><div><br>
<<a href="https://github.com/uwiger/__gproc" target="_blank">https://github.com/uwiger/__<u></u>gproc</a><br>
<<a href="https://github.com/uwiger/gproc" target="_blank">https://github.com/uwiger/<u></u>gproc</a>>><br>
<br>
<br>
<br>
On 03/16/2012 05:11 PM, AD wrote:<br>
<br>
Hello,<br>
<br>
I have cowboy setup and working to send/receive<br>
websockets over a<br>
socket connection. I am trying to figure out how to<br>
initiate a<br>
message<br>
server side over that channel (without it just being a<br>
reply). I am<br>
envisioning exposing a webservice on the cowboy service that<br>
would then<br>
push a message to all connected users (or maybe a subset<br>
based<br>
on some<br>
criteria).<br>
<br>
Does anyone know if this is possible and if so how to<br>
implement?<br>
<br>
Cheers,<br>
-AD<br>
<br>
<br></div>
______________________________<u></u>_____________________<div><br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br></div>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a>__<a href="http://erlang.org" target="_blank">erl<u></u>ang.org</a><br>
<mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>>><br>
<a href="http://erlang.org/mailman/____listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/____<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a>><div><br>
<br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>>><br>
<br>
<br>
<br>
--<br>
Loďc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
<br>
<br>
<br>
<br>
--<br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
<br>
<br>
</div></blockquote><div><div></div><div>
<br>
<br>
-- <br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
</div></div></blockquote></div><br></div></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br>