Skip to content

Commit f59a86a

Browse files
authored
feat(#11): Allow publishing and subscribing from Erlang through nrte (#12)
Closes #11.
1 parent 4da5544 commit f59a86a

6 files changed

Lines changed: 39 additions & 13 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ An OTP application to send and receive real time events over HTTP connections.
55
## Status
66
[![GitHub branch checks state](https://github.com/nomasystems/nrte/actions/workflows/ci.yml/badge.svg)](https://github.com/nomasystems/nrte/actions/workflows/ci.yml)
77

8-
98
## Prerequisites
109

1110
![Min. OTP version](https://img.shields.io/badge/min._OTP-25.3.2-blue)
@@ -81,7 +80,17 @@ nrte_auth(_Headers) ->
8180

8281
## Bypassing the HTTP connections
8382

84-
All events are published and received through the [`erlbus`](https://github.com/cabol/erlbus) library. It is possible to directly use it along the HTTP interfaces by publishing or subscribing to the same topics.
83+
Messages can also be published via `nrte:publish/2` and subscribed from `nrte:subscribe/1`. In the latter case, the caller will receive messages in the form of `{nrte_message, Data}`:
84+
```erl
85+
> nrte:subscribe([<<"example">>]).
86+
ok
87+
88+
> nrte:publish(<<"example">>, <<"my-message">>).
89+
ok
90+
91+
> receive {nrte_message, Data} -> Data end.
92+
<<"example;my-message">>
93+
```
8594

8695
## Support
8796

src/nrte.erl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
%%% APPLICATION EXPORTS
1818
-export([start/2, stop/1]).
1919

20+
%%% EXTERNAL EXPORTS
21+
-export([publish/2, subscribe/1]).
22+
2023
%%%-----------------------------------------------------------------------------
2124
%%% APPLICATION EXPORTS
2225
%%%-----------------------------------------------------------------------------
@@ -40,3 +43,22 @@ start(_, _) ->
4043
stop(_) ->
4144
cowboy:stop_listener(nrte_listener),
4245
ok.
46+
47+
%%%-----------------------------------------------------------------------------
48+
%%% EXTERNAL EXPORTS
49+
%%%-----------------------------------------------------------------------------
50+
-spec publish(binary(), binary()) -> ok.
51+
publish(Topic, Message) ->
52+
ExpandedTopics = expand_topic(Topic),
53+
lists:foreach(fun(T) -> ebus:pub(T, Message) end, ExpandedTopics).
54+
55+
-spec subscribe([iodata()]) -> ok.
56+
subscribe(TopicList) ->
57+
nrte_ebus_handler:subscribe(TopicList).
58+
59+
%%%-----------------------------------------------------------------------------
60+
%%% INTERNAL FUNCTIONS
61+
%%%-----------------------------------------------------------------------------
62+
expand_topic(Topic) ->
63+
Subtopics = [binary:part(Topic, {0, Pos}) || {Pos, _} <- binary:matches(Topic, <<":">>)],
64+
[Topic | Subtopics].

src/nrte_ebus_handler.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ handle_message(Message, Topic, Pid) ->
4444
Template,
4545
Replacements
4646
),
47-
Pid ! {ebus_message, Data}.
47+
Pid ! {nrte_message, Data}.

src/nrte_eventsource.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
init(Req, Opts) ->
2727
case nrte_auth:authorization(Req, subscribe) of
2828
{authorized, TopicList} ->
29-
nrte_ebus_handler:subscribe(TopicList),
29+
nrte:subscribe(TopicList),
3030
Req2 = cowboy_req:stream_reply(200, #{<<"content-type">> => ?CONTENT_TYPE}, Req),
3131
{cowboy_loop, Req2, Opts};
3232
{unauthorized, Req2} ->
3333
{stop, Req2, Opts}
3434
end.
3535

36-
info({ebus_message, Data}, Req, Opts) ->
36+
info({nrte_message, Data}, Req, Opts) ->
3737
ok = cowboy_req:stream_events(#{data => Data}, nofin, Req),
3838
{ok, Req, Opts}.

src/nrte_rest.erl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ init(Req, Opts) ->
3131
%%%-----------------------------------------------------------------------------
3232
%%% INTERNAL FUNCTIONS
3333
%%%-----------------------------------------------------------------------------
34-
expand_topic(Topic) ->
35-
Subtopics = [binary:part(Topic, {0, Pos}) || {Pos, _} <- binary:matches(Topic, <<":">>)],
36-
[Topic | Subtopics].
37-
3834
init_authorized(#{path := <<"/message">>} = Req, Opts, [<<>>]) ->
3935
{stop, cowboy_req:reply(400, #{}, <<"Topics required">>, Req), Opts};
4036
init_authorized(#{path := <<"/message">>} = Req, Opts, TopicList) ->
41-
ExpandedTopics = lists:append([expand_topic(T) || T <- TopicList]),
4237
{ok, Body, Req2} = cowboy_req:read_body(Req),
43-
lists:foreach(fun(T) -> ebus:pub(T, Body) end, ExpandedTopics),
38+
lists:foreach(fun(T) -> nrte:publish(T, Body) end, TopicList),
4439
{ok, cowboy_req:reply(200, #{}, <<>>, Req2), Opts};
4540
init_authorized(Req, Opts, _TopicList) ->
4641
{stop, cowboy_req:reply(404, #{}, <<>>, Req), Opts}.

src/nrte_websocket.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
init(Req, Opts) ->
2424
case nrte_auth:authorization(Req, subscribe) of
2525
{authorized, TopicList} ->
26-
nrte_ebus_handler:subscribe(TopicList),
26+
nrte:subscribe(TopicList),
2727
{cowboy_websocket, Req, []};
2828
{unauthorized, Req2} ->
2929
{stop, Req2, Opts}
@@ -32,7 +32,7 @@ init(Req, Opts) ->
3232
websocket_handle(_Data, State) ->
3333
{ok, State}.
3434

35-
websocket_info({ebus_message, Data}, State) ->
35+
websocket_info({nrte_message, Data}, State) ->
3636
{reply, {text, Data}, State};
3737
websocket_info(_Info, State) ->
3838
{ok, State}.

0 commit comments

Comments
 (0)