Permalink
Browse files

adds configuration options for misultin port and message handler

  • Loading branch information...
1 parent a5959b4 commit 18ecb448bad3a234af492fbeda74f235bf00b705 @videlalvaro committed May 15, 2011
Showing with 36 additions and 10 deletions.
  1. +24 −3 README.md
  2. +2 −1 ebin/rabbitmq_websockets.app
  3. +5 −2 src/rabbit_websockets_util.erl
  4. +5 −4 src/rabbit_websockets_worker.erl
View
@@ -1,6 +1,6 @@
# RabbitMQ Websockets Plugin #
-This plugin exposes a Websockets for RabbitMQ.
+This plugin exposes Websockets for RabbitMQ.
The user connects to the host where RabbitMQ is running, using default port `8080`.
@@ -20,13 +20,15 @@ Get the `rabbitmq-public-umbrella`
Get the [misultin_wrapper](https://github.com/videlalvaro/misultin_wrapper) to support Websockets:
+Inside the `rabbitmq-public-umbrella` directory do:
+
$ git clone git://github.com/videlalvaro/misultin_wrapper.git
-Clone this repository:
+Then clone this repository:
$ git clone git://github.com/videlalvaro/rabbitmq-websockets.git
-Once you have the code you can move into the `rabbitmq-websockets` and test the plugin with the broker:
+Once you have the code you can move into the `rabbitmq-websockets` directory and test the plugin with the broker:
$ make run-in-broker
@@ -42,6 +44,25 @@ You can publish test messages by calling the following helper function inside th
All three parameters are binaries.
+## Configuration ##
+
+This plugin has two parameters that affect it's behavior:
+
+`misultin_port`: The port Misultin should listen to. Default value is `8080`.
+
+`message_handler`: an Erlang tuple with two atoms like {module, function}. Default value is `{rabbit_websockets_util, basic_handler}`.
+
+You can modify such settings on your `rabbitmq.config` file like this:
+
+ [
+ {rabbit, [
+ ...
+ %% list of RabbitMQ options
+ ]},
+ {rabbitmq_websockets, [ {misultin_port, 8081},
+ {message_handler, {my_module, my_function}} ]}
+ ].
+
## License ##
See LICENSE.md
@@ -10,5 +10,6 @@
]},
{registered, []},
{mod, {rabbit_websockets, []}},
- {env, [{misultin_port, 8080}]},
+ {env, [ {misultin_port, 8080},
+ {message_handler, {rabbit_websockets_util, basic_handler}} ]},
{applications, [kernel, stdlib, rabbit, amqp_client]}]}.
@@ -1,6 +1,6 @@
-module(rabbit_websockets_util).
--export([publish_msg/3]).
+-export([publish_msg/3, basic_handler/1]).
-include_lib("amqp_client/include/amqp_client.hrl").
@@ -9,4 +9,7 @@ publish_msg(Exchange, Msg, RKey) ->
{ok, Channel} = amqp_connection:open_channel(Connection),
Publish = #'basic.publish'{exchange = Exchange, routing_key = RKey},
amqp_channel:call(Channel, Publish, #amqp_msg{payload = term_to_binary(Msg)}),
- ok.
+ ok.
+
+basic_handler(Msg) ->
+ binary_to_term(Msg).
@@ -10,7 +10,7 @@
-record(state, {connection, port}).
-record(http_state, {req}).
--record(websocket_state, {ws, conn, consumer}).
+-record(websocket_state, {ws, conn, consumer, handler}).
start_link(Port) ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [Port], []).
@@ -25,12 +25,13 @@ stop() ->
init([Port]) ->
process_flag(trap_exit, true),
{ok, Connection} = amqp_connection:start(#amqp_params_direct{}),
+ {ok, Fun} = application:get_env(message_handler),
misultin:start_link([{port, Port},
{loop, fun(Req) -> handle_http(#http_state{req=Req}) end},
{ws_loop,
fun(Ws) ->
- handle_websocket(#websocket_state{ws=Ws, conn=Connection})
+ handle_websocket(#websocket_state{ws=Ws, conn=Connection, handler=Fun})
end},
{ws_autoexit, false}]),
erlang:monitor(process, misultin),
@@ -101,10 +102,10 @@ css_root() ->
% Websockets Handling Implementation
% ----------------------------------
-handle_websocket(#websocket_state{ws=Ws, conn=Connection} = State) ->
+handle_websocket(#websocket_state{ws=Ws, conn=Connection, handler={M, F}} = State) ->
receive
{amqp_msgs, Msg} ->
- Ws:send(binary_to_term(Msg)),
+ Ws:send(M:F(Msg)),
handle_websocket(State);
{browser, Data} ->
{E, R} = parse_data(Data),

0 comments on commit 18ecb44

Please sign in to comment.