Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wrapper for nodejs websocket server. #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/chord/channels.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#?(:clj
[clojure.core.async.impl.protocols :as p]
:cljs
[cljs.core.async.impl.protocols :as p]))
[cljs.core.async.impl.protocols :as p])

[chord.format :as cf])

#?(:cljs (:require-macros [cljs.core.async.macros :refer [go-loop]])))

Expand Down Expand Up @@ -50,3 +52,20 @@
(p/close! write-ch)
(when on-close
(on-close)))))

(defn- on-close [ws ws-ch]
#?(:clj (http/on-close ws (fn [_] (close! ws-ch)))
:cljs (.on ws "close" #(close! ws-ch))))

(defn wrap-websocket [socket {:keys [read-ch write-ch] :as opts} & [cleanup]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you coalesce the opts map and the cleanup function, so that we don't have multiple options parameters? e.g. (defn wrap-websocket [socket {:keys [read-ch write-ch on-close] :as opts}] ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a bit more thought, I wonder why we leave this up to the implementer at all? We only have two types of connections, so I can clean up automatically.

(let [{:keys [read-ch write-ch]}
(-> {:read-ch (or read-ch (chan))
:write-ch (or write-ch (chan))}
(cf/wrap-format (dissoc opts :read-ch :write-ch)))]

(read-from-ws! socket read-ch)
(write-to-ws! socket write-ch)

(let [ws-ch (bidi-ch read-ch write-ch {:on-close cleanup})]
(on-close socket ws-ch)
ws-ch)))
34 changes: 8 additions & 26 deletions src/chord/http_kit.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
(ns chord.http-kit
(:require [clojure.core.async :as a :refer [chan <! >! close! go-loop]]
[org.httpkit.server :as http]
[chord.channels :refer [read-from-ws! write-to-ws! bidi-ch]]
[chord.format :as cf]))

(defn- on-close [ws read-ch write-ch]
(http/on-close ws
(fn [_]
;; TODO support status?
(close! read-ch)
(close! write-ch))))

(defn core-async-ch [httpkit-ch {:keys [read-ch write-ch format] :as opts}]
(let [{:keys [read-ch write-ch]} (-> {:read-ch (or read-ch (chan))
:write-ch (or write-ch (chan))}
(cf/wrap-format (dissoc opts :read-ch :write-ch)))]

(read-from-ws! httpkit-ch read-ch)
(write-to-ws! httpkit-ch write-ch)
(on-close httpkit-ch read-ch write-ch)

(bidi-ch read-ch write-ch {:on-close #(when (http/open? httpkit-ch)
(http/close httpkit-ch))})))
[chord.channels :refer [wrap-websocket]]))

(defmacro with-channel
"Extracts the websocket from the request and binds it to 'ch-name' in the body
Expand Down Expand Up @@ -52,18 +32,20 @@
(when-let [msg (<! the-ws)]
(println msg)
(recur))))"

[req ch-name & [opts & body]]

(let [opts? (and (or (map? opts)
(:opts (meta opts)))
(seq body))
body (cond->> body
(not opts?) (cons opts))
opts (when opts? opts)]

`(http/with-channel ~req httpkit-ch#
(let [~ch-name (core-async-ch httpkit-ch# ~opts)]
(let [~ch-name (wrap-websocket httpkit-ch# ~opts
#(when (http/open? httpkit-ch#)
(http/close httpkit-ch#)))]
~@body))))

(defn wrap-websocket-handler
Expand All @@ -74,7 +56,7 @@
handler - (required) Ring-compatible handler
opts - (optional) Options for the WebSocket channel - same options as for `with-channel`"
[handler & [opts]]

(fn [req]
(if (:websocket? req)
(with-channel req ws-conn
Expand Down
42 changes: 42 additions & 0 deletions src/chord/node.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns chord.node
(:require [cljs.nodejs :as nodejs]
[chord.channels :as channels]
[chord.format :as format]))

(defn ws-server [ws-opts {:keys [read-ch write-ch :as chord-opts]} handler]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a more consistent signature here is (defn ws-server [handler {:keys [read-ch write-ch ws-opts]}]...) ? (looking at wrap-websocket-handler, above)

(when-let [ws (nodejs/require "ws")]
(let [server (ws.Server. (clj->js ws-opts))]
(.on server "connection"
(fn [socket request]
(let [ws-ch (channels/wrap-websocket socket chord-opts #(.close socket))]
(handler ws-ch request))))
server)))

(comment
"Plain example"

(defn handler [ws-ch req]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great examples - could you also put them in the README to make them more discoverable?

(if (authorized? req)
(async/put! ws-ch "Welcome!")
(do
(async/put! ws-ch "Denied!")
(async/close! ws-ch)))
(async/go
(println (async/<! ws-ch))
'...))

(def server (chord.node/ws-server {:port 8080} {:format :edn}) handler)

"Express Example"

(def express (nodejs/require "express"))
(def https (nodejs/require "https"))
(def app (express))
'...

;; Note: SSL is inherited from the webserver, so using http gives you ws, and
;; https gives you wss.
(def server (.createServer https app))

(def ws-server
(chord.node/ws-server server {:format :json} handler)))