
hub.pl -- Manage a hub for websocketsThis library manages a hub that consists of clients that are connected using a websocket. Messages arriving at any of the websockets are sent to the event queue of the hub. In addition, the hub provides a broadcast interface. A typical usage scenario for a hub is a chat server A scenario for realizing an chat server is:
read
or write and Error is the Prolog I/O exception.
The thread(s) can talk to clients using two predicates:
A hub consists of (currenty) four message queues and a simple dynamic fact. Threads that are needed for the communication tasks are created on demand and die if no more work needs to be done.
hub_create(+Name, -Hub, +Options) is detthread(s) can listen.After creating a hub, the application normally creates a thread that listens to Hub.queues.event and exposes some mechanisms to establish websockets and add them to the hub using hub_add/3.
current_hub(?Name, ?Hub) is nondet
hub_add(+Hub, +WebSocket, ?Id) is det
wait_for_set(+Set0, -Left, -Ready, +Max) is det[private]
wait_timeout(+WaitForList, +Max, -TimeOut) is det[private]
get_messages(+Queue, +Max, -List) is det[private]
io_read_error(+WebSocket, +Error)[private]
io_write_error(+WebSocket, +Message, +Error)[private]left message and pass the error
such that the client can re-send it when appropriate.
hub_send(+ClientId, +Message) is semidet
hub_broadcast(+Hub, +Message) is det
hub_broadcast(+Hub, +Message, :Condition) is detcall(Condition, Id) succeeds. Note that this process is
asynchronous: this predicate returns immediately after putting
all requests in a broadcast queue. If a message cannot be
delivered due to a network error, the hub is informed through
io_error/3.
broadcast_from_queues(+Hub, +Options) is det[private]
broadcast_from_queue(+Queue, +Options) is det[private]
hub_thread(:Goal, +Hub, +Task) is det[private]hub(thread) is enabled.
hub_broadcast(+Hub, +Message) is det
hub_broadcast(+Hub, +Message, :Condition) is detcall(Condition, Id) succeeds. Note that this process is
asynchronous: this predicate returns immediately after putting
all requests in a broadcast queue. If a message cannot be
delivered due to a network error, the hub is informed through
io_error/3.