taoensso / sente

Realtime web comms library for Clojure/Script
https://www.taoensso.com/sente
Eclipse Public License 1.0
1.73k stars 193 forks source link

Ring middleware is not working with aleph adapter #350

Closed g7s closed 1 year ago

g7s commented 4 years ago

Currently the aleph adapter returns a manifold Deferred response and ring middleware (such as ring.middleware.session) cannot work with such a value. Changing this deftype to:

(deftype AlephAsyncNetworkChannelAdapter []
  i/IServerChanAdapter
  (ring-req->server-ch-resp [sch-adapter ring-req callbacks-map]
    (let [{:keys [on-open on-close on-msg _on-error]} callbacks-map
          ws? (websocket-req? ring-req)]
      (if-let [s (and ws? (try @(aleph/websocket-connection ring-req)
                               (catch Exception e
                                 nil)))]
        (do
          (when on-msg   (s/consume     (fn [msg] (on-msg   s ws? msg)) s))
          (when on-close (s/on-closed s (fn []    (on-close s ws? nil))))
          (when on-open  (do                     (on-open  s ws?)))
          {:body s})
        (let [s (s/stream)]             ; sch
          (when on-close (s/on-closed s (fn [] (on-close s ws? nil))))
          (when on-open  (do                  (on-open  s ws?)))
          {:body s})))))

seems to work, but someone more qualified should take a better look

ptaoussanis commented 1 year ago

@g7s Hi Gerasimos, thanks for pinging about this. Unfortunately I'm not familiar with the Aleph API or possible tradeoffs with this change.

Since it's been a few years and no one else has given any input, I'll just incorporate your suggestion in the next pre-release and if anyone has any issues then we can revert.

ptaoussanis commented 1 year ago

Closing, this change will be incorporated in forthcoming v1.18 release.

g7s commented 1 year ago

Thank you Peter 🚀