nova_pubsub (nova v0.14.3)

View Source

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

broadcast(Channel, Topic, Message)

-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.

get_local_members(Channel)

-spec get_local_members(Channel :: atom()) -> [pid()].

Works the same way as get_members/1 but returns only members on the same node.

get_members(Channel)

-spec get_members(Channel :: atom()) -> [pid()].

Returns all members for a given channel

join(Channel)

-spec join(Channel :: atom()) -> ok.

Joining a channel with the calling process. Always returns ok

join(Channel, Pid)

-spec join(Channel :: atom(), Pid :: pid()) -> ok.

Same as join/1 but with a specified process.

leave(Channel)

-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.

leave(Channel, Pid)

-spec leave(Channel :: atom(), Pid :: pid()) -> ok | not_joined.

Same as leave/1 but with a specified process.

local_broadcast(Channel, Topic, Message)

-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.