Fork me on GitHub
#aleph
<
2023-09-16
>
Giorgos Makris18:09:24

Hello! Is this the right place to ask for some help with aleph?

Giorgos Makris18:09:53

I'm trying to build something with websockets and my code right now looks like this:

(ns project.server
  (:require
   [reitit.ring :refer [router ring-handler]]
   [aleph.http :as http]
   [manifold.stream :as s]
   [manifold.deferred :as d]
   [clojure.tools.logging :as log]))

(defn join-session
  [request]
  (with-path-params [session-id] request
    (let [session-uuid (parse-uuid session-id)]
      (if-let [session (@sessions session-uuid)]
        (do
          (log/infof "Connected in session '%s'" session-id)
          (-> request
              http/websocket-connection
              (d/chain #(join-session! % session-uuid session))))
        (do
          (log/warnf "Rejected for session '%s': Session not found!" session-id)
          (-> request
              http/websocket-connection
              (http/websocket-close! 3404 "No session found!"))
          {:status 404 :body "Session not found"})))))

(def routes
  [["/ping" {:get (fn [_]
                    {:status 200 :body "pong"})}]
   ["/session" {:get start-session}]
   ["/session/:session-id" {:get join-session}]])

(def app (-> routes
             router
             ring-handler))

(defn start-server
  []
  (when-not @server
    (let [port 8080]
      (reset! server
              (http/start-server #'app
                                 {:port port}))
      (log/infof "Started server at port %d" port))))

Giorgos Makris18:09:12

My issue seems to be trying to initiate the websocket connection:

java.lang.ClassCastException: class manifold.deferred.Deferred cannot be cast to class manifold.stream.core.IEventSink (manifold.deferred.Deferred is in unnamed module of loader clojure.lang.DynamicClassLoader @3e9b3b3c; manifold.stream.core.IEventSink is in unnamed module of loader clojure.lang.DynamicClassLoader @73db7df0)
	at aleph.http.websocket.common$websocket_close_BANG_.invokeStatic(common.clj:114) ~[?:?]
	at aleph.http.websocket.common$websocket_close_BANG_.invoke(common.clj:106) ~[?:?]
	at aleph.http$websocket_close_BANG_.invokeStatic(http.clj:293) ~[?:?]
	at aleph.http$websocket_close_BANG_.invoke(http.clj:278) ~[?:?]
	at aleph.http$websocket_close_BANG_.invokeStatic(http.clj:290) ~[?:?]
	at aleph.http$websocket_close_BANG_.invoke(http.clj:278) ~[?:?]
	at project.server$join_session.invokeStatic(form-init2560565438093657059.clj:77) ~[?:?]
	at project.server$join_session.invoke(form-init2560565438093657059.clj:63) ~[?:?]
	at project.server$wrap_deferred$fn__22637.invoke(server.clj:34) ~[?:?]
	at reitit.ring$ring_handler$fn__2363.invoke(ring.cljc:351) ~[?:?]
	at clojure.lang.AFn.applyToHelper(AFn.java:154) ~[clojure-1.11.1.jar:?]
	at clojure.lang.AFn.applyTo(AFn.java:144) ~[clojure-1.11.1.jar:?]
	at clojure.lang.AFunction$1.doInvoke(AFunction.java:31) ~[clojure-1.11.1.jar:?]
	at clojure.lang.RestFn.invoke(RestFn.java:408) ~[clojure-1.11.1.jar:?]
	at clojure.lang.Var.invoke(Var.java:384) ~[clojure-1.11.1.jar:?]
	at aleph.http.server$handle_request$fn__13744$f__3077__auto____13745.invoke(server.clj:166) ~[?:?]
	at clojure.lang.AFn.run(AFn.java:22) ~[clojure-1.11.1.jar:?]
	at io.aleph.dirigiste.Executor$Worker$1.run(Executor.java:62) ~[dirigiste-1.0.3.jar:?]
	at manifold.executor$thread_factory$reify__2955$f__2956.invoke(executor.clj:70) ~[?:?]
	at clojure.lang.AFn.run(AFn.java:22) ~[clojure-1.11.1.jar:?]
	at java.lang.Thread.run(Thread.java:1623) [?:?]

Giorgos Makris18:09:38

I looked through the examples for websockets but I must be missing something really obvious

oyakushev19:09:50

I havent' worked with websockets in Aleph, but it seems like http/websocket-connection returns a deferred, so when you close it, you have to chain it, not just use a threading macro

oyakushev19:09:05

(-> request
              http/websocket-connection
              (d/chain #(http/websocket-close! % 3404 "No session found!")))

Giorgos Makris19:09:26

ah, that is reasonable, i'll give it a try. thanks 😄

Giorgos Makris19:09:46

that and forgetting to add (d/chain #(s/connect % %))solved my issue, thanks again

🔥 2
👍 1
👏 1