Thanks, getting some stacktraces though...<div><div><br></div><div>** Handler ad_test terminating in websocket_init/3</div><div> for the reason error:badarg</div><div><br></div><div>i have </div><div><br></div><div><div>
<i>-define(WSBroadcast,"wsbroadcast").</i></div></div><div><br></div><div>then in websocket_init</div><div><br></div><div><div><i>gproc:reg({p, l, {?MODULE, ?WSBroadcast}})</i></div></div><div><i><br></i></div><div>
then in my handle() method in cowboy for a certain path</div><div><br></div><div><div><i>{URLPath,_Req2} = cowboy_http_req:path(Req),</i></div><div><i> case lists:nth(1,URLPath) of</i></div><div><i> <span class="Apple-tab-span" style="white-space:pre"> </span><<"sendit">> -> </i></div>
<div><i> <span class="Apple-tab-span" style="white-space:pre"> </span>%io:format("Sending websocket !!~n"),</i></div><div><i> <span class="Apple-tab-span" style="white-space:pre"> </span>Msg = "Test broadcast",</i></div>
<div><i><span class="Apple-tab-span" style="white-space:pre"> </span>gproc:send({p, l, {?MODULE,?WSBroadcast}}, {self(), {?MODULE,?WSBroadcast}, Msg});</i></div></div><div><i><br></i></div><div>then in websocket_info</div>
<div><br></div><div><div>w<i>ebsocket_info({_PID,{_MODULE,_WSBroadcast},Msg},Req,State) -></i></div><div><i> <span class="Apple-tab-span" style="white-space:pre"> </span>{reply, {text, <<Msg>>}, Req, State, hibernate}.</i></div>
</div><div><br></div>Any thoughts ? Looks like something with ets:insert</div><div><br></div><div><div>** Stacktrace: [{ets,insert_new,</div><div> [gproc,</div><div> [{{{p,l,{ad_test,"wsbroadcast"}},<0.153.0>},</div>
<div> <0.153.0>,undefined},</div><div> {{<0.153.0>,{p,l,{ad_test,"wsbroadcast"}}},[]}]],</div><div> []},</div></div><div><br></div><div>Thanks again for the help</div>
<div>-AD</div><div><br></div><div><div class="gmail_quote">On Fri, Mar 16, 2012 at 2:06 PM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu">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">
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 the first arg of websocket_info/3. All messages the websocket process receives are given to you in websocket_info/3.<br>
<br>
Didn't try the new pubsub, it looks interesting though.<div class="im"><br>
<br>
On 03/16/2012 07:01 PM, AD wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
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>
<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 <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br></div><div class="im">
<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 itself<br>
using gproc[1] (or through a central gen_server) inside the<br>
websocket_init/3 callback, and then have your service push messages<br>
to all registered handlers which you can then catch in websocket_info/3.<br>
<br>
I mention gproc because it's infinitely easier with it, as you only<br>
have to register all your processes under one property and then send<br>
a message to that property which will multicast it to all registered<br>
processes. And when your websocket closes, gproc takes care of<br>
removing your process from the list of registered processes, so you<br>
really have to worry only about 2 lines of code to do 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> <<a href="https://github.com/uwiger/gproc" target="_blank">https://github.com/uwiger/<u></u>gproc</a>><div class="im">
<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 websockets over a<br>
socket connection. I am trying to figure out how to initiate a<br>
message<br>
server side over that channel (without it just being a 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 based<br>
on some<br>
criteria).<br>
<br>
Does anyone know if this is possible and if so how to implement?<br>
<br>
Cheers,<br>
-AD<br>
<br>
<br></div>
______________________________<u></u>___________________<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>
<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><div class="im"><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>
</div></blockquote><font color="#888888">
<br>
<br>
-- <br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
</font></blockquote></div><br></div>