Liking cljdoc? Tell your friends :D

rooms - collaborative state with websockets

Clojars Project

[rooms "0.5.1-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

const ws = new WebSocket("ws://localhost:8080/room/demo"); // note room id

ws.onopen = () => {
  setTimeout(() => {
    ws.send(JSON.stringify({greeting: "hello", from: navigator.userAgent}));
  }, 1000);
};

ws.onmessage = (msg) => {
  console.log(JSON.parse(msg.data));
}

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 ❤️

Dubsado CRM

License

Copyright © 2019 Duncan Smith

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close