Skip to content

Instantly share code, notes, and snippets.

@denen99
Created March 20, 2012 20:07
Show Gist options
  • Save denen99/2140767 to your computer and use it in GitHub Desktop.
Save denen99/2140767 to your computer and use it in GitHub Desktop.
% snippet from pubsub_srv.erl
%-----------
start_link() ->
{ok,PID} = eredis_sub:start_link(),
register(eredispid,PID),
_Receiver = spawn_link(fun () ->
eredis_sub:controlling_process(PID),
receiver(PID)
end),
io:format("Eredis server started~n"),
{ok, PID}.
receiver(Sub) ->
receive
Msg ->
io:format("received ~p~n", [Msg]),
eredis_sub:ack_message(Sub),
?MODULE:receiver(Sub)
end.
subscribe_channel(Channel) ->
io:format("received request to subscribe to ~p~n",[Channel]),
Sub = whereis(eredispid),
io:format("Found PID ~p for subscription~n",[Sub]),
eredis_sub:subscribe(Sub, [Channel]).
%snippet from cowboy_callback.erl
%----------------------------------
handle(Req,State) ->
case cowboy_http_req:qs_val(<<"channel">>,Req) of
{undefined,_Req2} ->
io:format("Sorry missing channel query param"),
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"Sorry, invalid channel">>,Req);
{Channel,_Req2} ->
io:format("Subscribing to channel ~p~n",[Channel]),
erltest1_pubsub_srv:subscribe_channel(Channel),
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"You subscribed to a channel!">>,Req);
_ ->
io:format("Dont know what to do~n"),
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"Sorry dont know what to do">>,Req)
end,
{ok, Req3, State}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment