Fork me on GitHub

Hello I want to use luminus + reitit + aleph for websocket application. But I cannot find how to deal with create websocket. So. please let me know a sample program or lecture.


With compojure you can do something like

(defroutes web-socket-routes
           ;; edit Websocket server endpoint
           (GET "/editable" [:as req]
             (let [ch (ncc/hijack! req true)
                   uid (get-user req)]
               (if (and (is-editor uid) (ncc/websocket-upgrade! ch true))
                   (ncc/add-aggregated-listener! ch (* 5 1024 1024)
                                                 {:on-open    (fn [ch] (on-open! ch uid))
                                                  :on-message (fn [ch msg] (on-message! ch uid msg))
                                                  :on-close   (fn [ch reason] (on-close! ch uid reason))
                                                  :on-error   (fn [_ status] (log/warn "error on edit web socket with status" status))})
                   {:status 200 :body ch})
                   (log/debug "user with uid" uid "tried to connect to the editable web socket, but doesn't have the rights")
                   (ncc/send-response! ch {:status 403}))))))
In this case I only create a websocket server site if a editor is already logged in, and have a map to define witch functions to call on events.

👍 4

It sounds so great! I'll use it now!