rooms - collaborative state with websockets
[rooms "0.5.2-SNAPSHOT"]
Rooms is a Clojure library that aims to simplify the process of building WebSocket apps.
A Room
is an agent-based state container that users can connect to via WebSockets. Incoming WebSocket messages are given to a function which updates the room state, which is then broadcast to connected users.
Usage
Server-side
(require '[rooms.demo :refer [start create-room!]])
(create-room!
"demo" ; room id
(fn [state msg] (assoc state :last-message msg)) ; state fn
(fn [state user] state) ; view fn
(start {:port 8080})
Client-side
connect('demo', {
onConnect() {
setTimeout(() => {
this.send(JSON.stringify({greeting: "hello", from: navigator.userAgent}));
}, 1000);
},
onMessage(msg) {
console.log(JSON.parse(msg.data));
}
});
function connect(roomId, {onConnect, onMessage}) {
const ws = new WebSocket('ws://localhost:8080/room/' + roomId);
ws.onopen = onConnect.bind(ws);
ws.onmessage = onMessage.bind(ws);
}
Check out rooms.demo
for a more comprehensive look, including a test page you can visit in your browser.
This project is graciously sponsored by Dubsado
License
Copyright © 2019 Duncan Smith
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.