https://github.com/nijohando/event.websocket
Websocket client for clojure integrated with nijohando/event
https://github.com/nijohando/event.websocket
clojure websocket-client
Last synced: 8 months ago
JSON representation
Websocket client for clojure integrated with nijohando/event
- Host: GitHub
- URL: https://github.com/nijohando/event.websocket
- Owner: nijohando
- License: epl-1.0
- Created: 2018-09-09T14:38:48.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-14T11:12:08.000Z (over 7 years ago)
- Last Synced: 2025-10-22T01:49:37.431Z (8 months ago)
- Topics: clojure, websocket-client
- Language: Clojure
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# event.websocket
[](https://clojars.org/jp.nijohando/event.websocket)
[](https://circleci.com/gh/nijohando/event.websocket)
Experimental websocket client integrated with [nijohando/event](https://github.com/nijohando/event) bus.
## Installation
#### Ligningen / Boot
```clojure
[jp.nijohando/event.websocket "0.1.2"]
```
#### Clojure CLI / deps.edn
```clojure
jp.nijohando/event.websocket {:mvn/version "0.1.2"}
```
## Usage
```clojure
(require '[jp.nijohando.event :as ev]
'[jp.nijohando.event.websocket :as ws]
'[clojure.core.async :as ca])
```
#### Bus integration
This library provides only 3 functions that are `client`, `connect!` and `disconnect!`.
Function `client` creates an event bus that acts as a websocket client.
```clojure
(def bus (ws/client))
```
Function `connect!` connects the bus with the websocket server.
```clojure
(ws/connect! bus "wss://echo.websocket.org")
```
Function `disconnect!` disconnects the session from the websocket server.
```clojure
(ws/disconnect! bus)
```
All other operations are channel based operations with [nijohando/event](https://github.com/nijohando/event) API.
### Listening to websocket events
Various events related to websocket can be read from the listener channel.
```clojure
(def bus (ws/client))
(def listener (ca/chan))
(ev/listen bus "/*" listener)
(ca/go-loop []
(when-some [{:keys [path value] :as event} (ca/ "connected!"
; "pong message arrived!"
```
### Sending messages
Messages can be sent via the emitter channel.
```clojure
(def bus (ws/client))
(def emitter (ca/chan))
(def listener (ca/chan))
(ev/emitize bus emitter)
(ev/listen bus ["/" ["connect"]
["message/text"]
["error"]] listener)
(ca/go-loop []
(when-some [{:keys [path value] :as event} (ca/! emitter (ev/event "/send/text" "hello!"))
"/message/text" (prn "echo message arrivded! " value)
"/error" (prn "error! " value))
(recur)))
(ws/connect! bus "wss://echo.websocket.org")
;=> "echo message arrivded! " "hello!"
```
## License
© 2018 nijohando
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.