View Source nova_pubsub (nova v0.10.4)

Pubsub system for Nova. It uses the pg/pg2 module.

Pubsub subsystem is started with Nova and does not need any additional configuration. It uses the pg/pg2 module depending on the version of OTP. It provides a simple way of distributing messages to a large set of receivers and exposes a simple set of functions for doing that.

A simple example of how to use pubsub in a ping/pong inspired game engine:

-module(test_module). -export([player1/0, player2/0, start_game/0]).

player1() -> spawn(fun() -> nova_pubsub:join(game_of_pong), game_loop(1, "pong", "ping").

player2() -> spawn(fun() -> nova_pubsub:join(game_of_pong), game_loop(2, "ping", "pong").

game_loop(Player, ExpectedMessage, Smash) -> receive ExpectedMessage -> io:format("Player ~d received ~s and returning ~s~n", [Player, ExpectedMessage, Smash]), nova_pubsub:broadcast(game_of_pong, "match1", Smash), game_loop(Player, ExpectedMessage, Smash); _ -> game_loop(Player, ExpectedMessage, Smash) end.

Summary

Functions

Broadcasts a message to all members of a channel. Topic is specified to differentiate messages within the same channel.
Works the same way as get_members/1 but returns only members on the same node.
Returns all members for a given channel
Joining a channel with the calling process. Always returns ok
Same as join/1 but with a specified process.
Leaves a channnel. Will return ok on success and not_joined if the calling process were not part of the channel.
Same as leave/1 but with a specified process.
Works in the same way as broadcast/3 but only for members in the same node.

Functions

Link to this function

broadcast(Channel, Topic, Message)

View Source
-spec broadcast(Channel :: atom(), Topic :: list() | binary(), Message :: any()) -> ok.
Broadcasts a message to all members of a channel. Topic is specified to differentiate messages within the same channel.
Link to this function

get_local_members(Channel)

View Source
-spec get_local_members(Channel :: atom()) -> [pid()].
Works the same way as get_members/1 but returns only members on the same node.
-spec get_members(Channel :: atom()) -> [pid()].
Returns all members for a given channel
-spec join(Channel :: atom()) -> ok.
Joining a channel with the calling process. Always returns ok
-spec join(Channel :: atom(), Pid :: pid()) -> ok.
Same as join/1 but with a specified process.
-spec leave(Channel :: atom()) -> ok | not_joined.
Leaves a channnel. Will return ok on success and not_joined if the calling process were not part of the channel.
-spec leave(Channel :: atom(), Pid :: pid()) -> ok | not_joined.
Same as leave/1 but with a specified process.
Link to this function

local_broadcast(Channel, Topic, Message)

View Source
-spec local_broadcast(Channel :: atom(), Topic :: list() | binary(), Message :: any()) -> ok.
Works in the same way as broadcast/3 but only for members in the same node.
-spec start() -> ok.