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>