Skip to content
This repository has been archived by the owner on Dec 11, 2018. It is now read-only.

Chat example for bullet #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Bullet Chat
============

To compile this example you need rebar in your PATH.

Type the following command:
```
$ rebar get-deps compile
```

You can then start the Erlang node with the following command:
```
./start.sh
```

Then point your browser to the indicated URL.

You can interrupt temporarily the node to check that Bullet
properly reconnects when something happens.
4 changes: 4 additions & 0 deletions examples/chat/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{deps, [
{bullet, ".*",
{git, "git://github.com/extend/bullet.git", "master"}}
]}.
15 changes: 15 additions & 0 deletions examples/chat/src/chat.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%% Feel free to use, reuse and abuse the code in this file.

{application, chat, [
{description, "Bullet chat example."},
{vsn, "1"},
{modules, []},
{registered, []},
{applications, [
kernel,
stdlib,
cowboy
]},
{mod, {chat_app, []}},
{env, []}
]}.
27 changes: 27 additions & 0 deletions examples/chat/src/chat.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%% Copyright (c) 2012, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(chat).

%% API.
-export([start/0]).

%% API.

start() ->
ok = application:start(crypto),
ok = application:start(ranch),
ok = application:start(cowlib),
ok = application:start(cowboy),
ok = application:start(chat).
28 changes: 28 additions & 0 deletions examples/chat/src/chat_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
%% Feel free to use, reuse and abuse the code in this file.

%% @private
-module(chat_app).
-behaviour(application).

%% API.
-export([start/2]).
-export([stop/1]).

%% API.

start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/", toppage_handler, []},
{"/bullet", bullet_handler, [{handler, stream_handler}]},
{"/static/[...]", cowboy_static, {priv_dir, bullet, []}}
]}
]),
{ok, _} = cowboy:start_http(http, 100,
[{port, 8080}], [{env, [{dispatch, Dispatch}]}]
),
register(chatty, spawn(subscriber,doit,[[]])),
chat_sup:start_link().

stop(_State) ->
ok.
23 changes: 23 additions & 0 deletions examples/chat/src/chat_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%% Feel free to use, reuse and abuse the code in this file.

%% @private
-module(chat_sup).
-behaviour(supervisor).

%% API.
-export([start_link/0]).

%% supervisor.
-export([init/1]).

%% API.

-spec start_link() -> {ok, pid()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% supervisor.

init([]) ->
Procs = [],
{ok, {{one_for_one, 10, 10}, Procs}}.
27 changes: 27 additions & 0 deletions examples/chat/src/stream_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%% Feel free to use, reuse and abuse the code in this file.

%% @doc Stream handler for chatting.
-module(stream_handler).

-export([init/4]).
-export([stream/3]).
-export([info/3]).
-export([terminate/2]).

init(_Transport, Req, _Opts, _Active) ->
whereis(chatty) ! {add, self()},
{ok, Req, self()}.

stream(<<"ping: ", Name/binary>>, Req, State) ->
io:format("ping ~p received~n", [Name]),
{reply, <<"pong">>, Req, State};

stream(Message, Req, State) ->
whereis(chatty) ! { send, Message },
{ok, Req, State}.

info(Info, Req, State) ->
{reply, Info, Req, State}.

terminate(_Req, _) ->
ok.
21 changes: 21 additions & 0 deletions examples/chat/src/subscriber.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(subscriber).

-compile(export_all).

doit(Subscribers) ->
receive
{add, Pid} ->
io:format("add pid ~p ~n", [Pid]),
NewSubscribers = [Pid| Subscribers],
doit(NewSubscribers);
{send, Message } ->
io:format("Sending message ~p to ~p ~n", [Message, Subscribers]),
lists:map( fun (Pid) -> Pid ! Message end, Subscribers),
doit(Subscribers);
{who, Sender} ->
io:format("Responsing with subscribers ~p to pid ~p", [Subscribers, Sender]),
Sender ! Subscribers,
doit(Subscribers);
terminate ->
ok
end.
54 changes: 54 additions & 0 deletions examples/chat/src/toppage_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
%% Feel free to use, reuse and abuse the code in this file.

%% @doc Main page of the chat application.
-module(toppage_handler).

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

init(_Transport, Req, []) ->
{ok, Req, undefined}.

handle(Req, State) ->
Body = <<"
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>Bullet Chat</title>
</head>

<body>
<script
src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\">
</script>
<script src=\"/static/bullet.js\"></script>
<script type=\"text/javascript\">
// <![CDATA[
$(document).ready(function(){
var bullet = $.bullet('ws://localhost:8080/bullet');
bullet.onmessage = function(e){
if (e.data != 'pong'){
$(\"#chats\").append(\"<p>\" + e.data + \"</p>\");

}
};

$(\"#send\").click( function () {
bullet.send($(\"#says\").val());
})
});
// ]]>
</script>
<div id=\"chats\"><p>Chats!</p></div>
<input type=\"text\" id=\"says\"/><button id=\"send\">send</button>
</body>
</html>
">>,
{ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],
Body, Req),
{ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
ok.
3 changes: 3 additions & 0 deletions examples/chat/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
erl -pa ebin deps/*/ebin -s chat \
-eval "io:format(\"Point your browser at http://localhost:8080~n\")."