Symple module

Server

Symple v4 server.

Server

#include <icy/symple/server.h>

Symple v4 server.

Accepts WebSocket connections, authenticates peers, manages rooms, and routes messages. Implements the Symple v4 protocol over native WebSocket.

Usage: smpl::Server server; server.start({.port = 4500});

// Optional: custom authentication server.Authenticate += & peer, const json::Value& auth, bool& allowed, std::vectorstd::string& rooms) { allowed = (auth.value("token", "") == "secret"); rooms.push_back("team-a"); };

The server also serves as an HTTP server, so you can serve static files (e.g. a web UI) on the same port.

Public Attributes

ReturnNameDescription
Signal< void(ServerPeer &, const json::Value &auth, bool &allowed, std::vector< std::string > &rooms)>AuthenticateCustom authentication hook. Set allowed to false to reject the peer. Append to rooms to assign team/group memberships. If not connected, all peers with a valid user field are accepted.
Signal< void(ServerPeer &)>PeerConnectedPeer authenticated and online.
Signal< void(ServerPeer &)>PeerDisconnectedPeer disconnected.

Authenticate

Signal< void(ServerPeer &, const json::Value &auth, bool &allowed, std::vector< std::string > &rooms)> Authenticate

Custom authentication hook. Set allowed to false to reject the peer. Append to rooms to assign team/group memberships. If not connected, all peers with a valid user field are accepted.


PeerConnected

Signal< void(ServerPeer &)> PeerConnected

Peer authenticated and online.


PeerDisconnected

Signal< void(ServerPeer &)> PeerDisconnected

Peer disconnected.

Public Methods

ReturnNameDescription
ServerConstructs a server using the given event loop.
ServerDeleted constructor.
voidstartStarts the server with the given options. Begins accepting WebSocket connections on opts.host:opts.port.
voidstartStarts the server with a custom HTTP factory for non-WebSocket requests. The Symple server handles WebSocket upgrades internally; any other HTTP request (e.g. static files, REST API) is delegated to this factory.
voidstopBroadcasts a shutdown notice to all peers, closes the listen socket, and releases all internal state. Safe to call more than once.
voidbroadcastBroadcast a message to all peers in a room (excluding sender).
voidbroadcastRoomsBroadcast to multiple rooms with per-recipient dedup.
boolsendToSend a message to a specific peer by session ID.
boolsendToUserSend a message to any peer with the given user name.
ServerPeer *getPeerGet a connected peer by session ID.
std::vector< ServerPeer * >getPeersInRoomGet all peers in a room.
size_tpeerCount constNumber of connected, authenticated peers.
voidaddVirtualPeerRegister a virtual peer that receives messages via callback.
voidremoveVirtualPeerRemove a virtual peer by session ID.
http::Server &httpServer inlineAccess the underlying HTTP server (e.g. to serve static files).
uv::Loop *loop const inlineEvent loop that owns the Symple server and all peer connections.

Server

Server(uv::Loop * loop)

Constructs a server using the given event loop.

Parameters


Server

Server(const Server &) = delete

Deleted constructor.


start

void start(const Options & opts)

Starts the server with the given options. Begins accepting WebSocket connections on opts.host:opts.port.

Parameters

  • opts Server configuration options.

start

void start(const Options & opts, std::unique_ptr< http::ServerConnectionFactory > httpFactory)

Starts the server with a custom HTTP factory for non-WebSocket requests. The Symple server handles WebSocket upgrades internally; any other HTTP request (e.g. static files, REST API) is delegated to this factory.

Parameters

  • opts Server configuration options.

  • httpFactory Factory for HTTP responders; may be nullptr.


stop

void stop()

Broadcasts a shutdown notice to all peers, closes the listen socket, and releases all internal state. Safe to call more than once.


broadcast

void broadcast(const std::string & room, const json::Value & msg, const std::string & excludeId)

Broadcast a message to all peers in a room (excluding sender).


broadcastRooms

void broadcastRooms(const std::unordered_set< std::string > & rooms, const json::Value & msg, const std::string & excludeId)

Broadcast to multiple rooms with per-recipient dedup.


sendTo

bool sendTo(const std::string & peerId, const json::Value & msg)

Send a message to a specific peer by session ID.


sendToUser

bool sendToUser(const std::string & user, const json::Value & msg)

Send a message to any peer with the given user name.


getPeer

ServerPeer * getPeer(const std::string & id)

Get a connected peer by session ID.


getPeersInRoom

std::vector< ServerPeer * > getPeersInRoom(const std::string & room)

Get all peers in a room.


peerCount

const

size_t peerCount() const

Number of connected, authenticated peers.


addVirtualPeer

void addVirtualPeer(const Peer & peer, const std::vector< std::string > & rooms, std::function< void(const json::Value &)> handler)

Register a virtual peer that receives messages via callback.

The virtual peer appears in presence broadcasts and is routable like any WebSocket-connected peer. Messages addressed to it are delivered via the callback instead of a WebSocket connection.

Parameters

  • peer Peer data (must have "id", "user", "name" fields).

  • rooms Rooms the virtual peer joins.

  • handler Called when a message is routed to this peer.


removeVirtualPeer

void removeVirtualPeer(const std::string & peerId)

Remove a virtual peer by session ID.


httpServer

inline

inline http::Server & httpServer()

Access the underlying HTTP server (e.g. to serve static files).


loop

const inline

inline uv::Loop * loop() const

Event loop that owns the Symple server and all peer connections.

Private Attributes

ReturnNameDescription
Options_opts
uv::Loop *_loop
std::unique_ptr< http::Server >_http
std::unique_ptr< PeerRegistry >_peerRegistry
std::unique_ptr< RoomIndex >_roomIndex
std::mutex_mutex
std::atomic< bool >_shuttingDown
std::unique_ptr< http::ServerConnectionFactory >_httpFallbackFallback factory for non-WebSocket HTTP requests.

_opts

Options _opts

_loop

uv::Loop * _loop = nullptr

_http

std::unique_ptr< http::Server > _http

_peerRegistry

std::unique_ptr< PeerRegistry > _peerRegistry

_roomIndex

std::unique_ptr< RoomIndex > _roomIndex

_mutex

std::mutex _mutex

_shuttingDown

std::atomic< bool > _shuttingDown {false}

_httpFallback

std::unique_ptr< http::ServerConnectionFactory > _httpFallback

Fallback factory for non-WebSocket HTTP requests.

Private Methods

ReturnNameDescription
voidonAuth
voidonMessage
voidonJoin
voidonLeave
voidonDisconnect
voidroute
booldeliver
booldeliverSerialized
voidsendPresenceSnapshot

onAuth

void onAuth(ServerPeer & peer, const json::Value & msg, std::unique_lock< std::mutex > & lock)

onMessage

void onMessage(ServerPeer & peer, json::Value msg)

onJoin

void onJoin(ServerPeer & peer, const std::string & room)

onLeave

void onLeave(ServerPeer & peer, const std::string & room)

onDisconnect

void onDisconnect(ServerPeer & peer, std::unique_lock< std::mutex > & lock)

route

void route(ServerPeer & sender, const json::Value & msg)

deliver

bool deliver(const std::string & peerId, const json::Value & msg)

deliverSerialized

bool deliverSerialized(const std::string & peerId, const char * data, size_t len, const json::Value & msg)

sendPresenceSnapshot

void sendPresenceSnapshot(ServerPeer & recipient, const std::unordered_set< std::string > & rooms, std::string_view excludeId)