Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure WebSocket Client

I have set up a WebSocket server using http-kit that should accept web socket connections. It is the basic example shown in the http-kit documentation.

The question is: How do you create a Clojure client that connects to it?

The client can be any Clojure http library, I don't really mind. I already know about Javascript clients, and I believe that Java has an API that I could use from Clojure. But what I am looking for is a Clojure library that supports websockets both for client and server. I saw nothing in http-kit code that would make it easy.

like image 614
user2263578 Avatar asked May 08 '13 17:05

user2263578


4 Answers

aleph has support for websockets on both server and client. It can take some time to get used to the asynchronous style and aleph's core abstractions, but it's a very good tool once you get the hang of it.

like image 152
amalloy Avatar answered Nov 17 '22 03:11

amalloy


Gniazdo is a WebSocket client for Clojure. It wraps the Jetty's implementation of the protocol.

like image 26
Jan Avatar answered Nov 17 '22 03:11

Jan


http-kit's client does not support WebSocket yet(I can't think of a good API for it). Aleph is a good option for this kind of use case. Another option is http.async.client, I've used it in http-kit's server's websocket unit test: here

like image 9
shenedu Avatar answered Nov 17 '22 04:11

shenedu


For those joining us in 2015: being new to this, I just spent a while trying out all the different options available, and it was pretty difficult to find a library that provides an easy way to set up a simple Clojure WebSocket client. (To be fair, it seems like it's not very common for a WebSocket client to be running in a non-browser/JavaScript context, which is why there seems to be so much emphasis on ClojureScript WebSocket clients.)

Although it is not well-documented, http.async.client ended up being the path of least resistance for me. I was able to successfully read streaming data from a WebSocket server and print it to the console by doing this:

(ns example.core
  (:require [http.async.client :as async]))

(def url "ws://localhost:1337")

(defn on-open [ws]
  (println "Connected to WebSocket."))

(defn on-close [ws code reason]
  (println "Connection to WebSocket closed.\n"
           (format "(Code %s, reason: %s)" code reason)))

(defn on-error [ws e]
  (println "ERROR:" e))

(defn handle-message [ws msg]
  (prn "got message:" msg))

(defn -main []
  (println "Connecting...")
  (-> (async/create-client)
      (async/websocket url
                       :open  on-open
                       :close on-close
                       :error on-error
                       :text  handle-message
                       :byte  handle-message))
  ;; Avoid exiting until the process is interrupted.
  (while true))

The infinite loop at the end is just to keep the process from ending. Until I press Ctrl-C, messages received from the socket are printed to STDOUT.

like image 8
Dave Yarwood Avatar answered Nov 17 '22 03:11

Dave Yarwood