Sigh, no. Thanks, its working perfectly now.<div><br></div><div>-AD<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">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 class="im"><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 class="im">
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 class="im"><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 class="im"><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 class="im">
<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 class="im"><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 class="im">
<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 class="im"><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 class="im"><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 class="im"><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 class="h5">
<br>
<br>
-- <br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
</div></div></blockquote></div><br></div>