resocket
resocket copied to clipboard
JDK11 Clojure WebSocket client with core.async API
resocket
JDK11 Clojure WebSocket client with core.async API
Features
- core.async API with
inputandoutputchannels for receiving/sending messages, andclosedpromise-chan with details about the closing of the connection. - Automatically send
pingframes to the server at given intervals, abort connections that do notpongwithin a certain timeout. - Close a connection by closing
outputchannel, abort within a certain timeout ifon-closeis not received from the server. - Specify
inputandoutputparsers to take/put your preferred data structure on the channels (maps, vecs, ...) and use the parser for json/edn parsing, etc. - Minimal dependencies, just
clojureandcore.async. reconnectorprocess to create new connections when the previous ones have been closed, returning a core.asyncconnectionschannel to take new connections from.- Small codebase, around ~300 LOC docstrings included.
Install
Leiningen/Boot
[io.github.bortexz/resocket "0.1.0"]
Clojure CLI/deps.edn
io.github.bortexz/resocket {:mvn/version "0.1.0"}
Usage
See the docs for more detailed information about all the options available.
Quick introduction
Connection:
(require '[bortexz.resocket :as resocket])
(let [{:keys [input output closed error]} (a/<!! (resocket/connection "ws://<service>" {}))]
(when-not error
(a/<!! input) ;; Take new messages
(a/>!! output "Hi from client") ;; Send messages
(a/close! output) ;; Close connection
(a/<!! closed)) ;; {:close-type :on-close :status 1000 :description ""}
)
Reconnector:
(let [{:keys [connections close closed?]} (resocket/reconnector {:get-url (constantly "ws://<service>")})]
(a/go-loop []
(when-let [conn (a/<! connections)] ;; get new connections until reconnector closed
(loop []
(when-let [v (a/<! (:input conn))]
;; Process messages
(recur)))
(recur)))
;; Somewhere else, when tired of receiving new connections
(a/close! close) ; Closes current connection (if any) and the reconnector
;; Listen to closed? in a different place than the connections handler
(when (a/<! closed?) "Reconnector has been closed.")
)
Credits
- hato as this library borrows some code from hato's websocket client.
License
Copyright © 2022 Alberto Fernandez
Distributed under the Eclipse Public License version 1.0.