\section{Group Communication}
\label{Sec::Group}

\lib supports publish/subscribe-based group communication.
Actors can join and leave groups and send messages to groups.

\begin{lstlisting}
std::string group_module = ...;
std::string group_id = ...;
auto grp = group::get(group_module, group_id);
self->join(grp);
self->send(grp, "test");
self->leave(grp);
\end{lstlisting}

\subsection{Anonymous Groups}
\label{Sec::Group::Anonymous}

Groups created on-the-fly with \lstinline^group::anonymous()^ can be used to coordinate a set of workers.
Each call to \lstinline^group::anonymous()^ returns a new, unique group instance.

\subsection{Local Groups}
\label{Sec::Group::Local}

The \lstinline^"local"^ group module creates groups for in-process communication.
For example, a group for GUI related events could be identified by \lstinline^group::get("local", "GUI events")^.
The group ID \lstinline^"GUI events"^ uniquely identifies a singleton group instance of the module \lstinline^"local"^.

\subsection{Remote Groups}
\label{Sec::Group::RemoteGroups}

To deploy groups in a network, one host can act as group server by publishing its local groups at any given port:

\begin{lstlisting}
void publish_local_groups(std::uint16_t port, const char* addr)
\end{lstlisting}

By calling \lstinline^group::get("remote", "<group>@<host>:<port>")^, other hosts are now able to connect to a remotely running group.
Please note that the group communication is no longer available once the server disconnects.
This implementation uses N-times unicast underneath.
It is worth mentioning that user-implemented groups can be build on top of IP multicast or overlay technologies such as Scribe to achieve better performance or reliability.

\clearpage
\subsection{Spawning Actors in Groups}
\label{Sec::Group::Spawn}

The function \lstinline^spawn_in_group^ can be used to create actors as members of a group.
The function causes the newly created actors to call \lstinline^join(...)^ immediately and before \lstinline^spawn_in_group^ returns. 
The usage of \lstinline^spawn_in_group^ is equal to \lstinline^spawn^, except for an additional group argument.
The group handle is always the first argument, as shown in the examples below.

\begin{lstlisting}
void fun1();
void fun2(int, float);
class my_actor1 : event_based_actor { /* ... */ };
class my_actor2 : event_based_actor {
  // ...
  my_actor2(const std::string& str) { /* ... */ }
};
// ...
auto grp = group::get(...);
auto a1 = spawn_in_group(grp, fun1);
auto a2 = spawn_in_group(grp, fun2, 1, 2.0f);
auto a3 = spawn_in_group<my_actor1>(grp);
auto a4 = spawn_in_group<my_actor2>(grp, "hello my_actor2!");
\end{lstlisting}
