[erlang-questions] Push a message to cowboy websockets

Loïc Hoguin essen@REDACTED
Fri Mar 16 19:06:57 CET 2012


Just like you would catch any message, you match:

websocket_info({Pid, {Module, WSBroadcast}, Msg}, Req, State)

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.

Didn't try the new pubsub, it looks interesting though.

On 03/16/2012 07:01 PM, AD wrote:
> Thanks, so in websocket/init
>
> gproc:reg({p, l, {?MODULE, WSBroadcast}}).
>
> Then in my webservice i can do
>
> Msg = "Test broadcast".
> gproc:send({p, l, {?MODULE,WSBroadcast}}, {self(),
> {?MODULE,WSBroadcast}, Msg}).
>
> How do you catch this in websocket_info/3 to formulate a reply?
>
> Also noticed a new pub/sub module for gproc not sure if this helps
> simplify some of this
> https://github.com/uwiger/gproc/blob/master/src/gproc_ps.erl
>
> Thanks!
> -AD
>
> On Fri, Mar 16, 2012 at 12:20 PM, Loïc Hoguin <essen@REDACTED
> <mailto:essen@REDACTED>> wrote:
>
>     Hey!
>
>     The general idea is to have your websocket handler register itself
>     using gproc[1] (or through a central gen_server) inside the
>     websocket_init/3 callback, and then have your service push messages
>     to all registered handlers which you can then catch in websocket_info/3.
>
>     I mention gproc because it's infinitely easier with it, as you only
>     have to register all your processes under one property and then send
>     a message to that property which will multicast it to all registered
>     processes. And when your websocket closes, gproc takes care of
>     removing your process from the list of registered processes, so you
>     really have to worry only about 2 lines of code to do everything you
>     need.
>
>     Good luck!
>
>     [1] https://github.com/uwiger/__gproc <https://github.com/uwiger/gproc>
>
>
>     On 03/16/2012 05:11 PM, AD wrote:
>
>         Hello,
>
>           I have cowboy setup and working to send/receive websockets over a
>         socket connection.  I am trying to figure out how to initiate a
>         message
>         server side over that channel (without it just being a reply).  I am
>         envisioning exposing a webservice on the cowboy service that
>         would then
>         push a message to all connected users (or maybe a subset based
>         on some
>         criteria).
>
>           Does anyone know if this is possible and if so how to implement?
>
>           Cheers,
>           -AD
>
>
>         _________________________________________________
>         erlang-questions mailing list
>         erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>         http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>
>
>
>
>     --
>     Loďc Hoguin
>     Erlang Cowboy
>     Nine Nines
>
>


-- 
Loïc Hoguin
Erlang Cowboy
Nine Nines



More information about the erlang-questions mailing list