How are you supposed to get the current user in a websocket connection in pedestal/jetty? Can’t seem to find any reference to the request at all in the connection request
...
::http/container-options {:context-configurator #(ws/add-ws-endpoints % ws-paths)}
...
(def ws-paths
{"/ws" {:on-connect (fn [e]
(let [[sess channel] ((ws/start-ws-connection new-ws-client) e)]
(println "CANT FIND A TOKEN OR ANYTHING HERE" e)
channel
))
:on-text (fn [msg] (log/info :msg (str "A client sent - " msg)))
:on-binary (fn [payload offset length] (log/info :msg "Binary Message!" :bytes payload))
:on-error (fn [t] (log/error :msg "WS Error happened" :exception t))
:on-close (fn [num-code reason-text]
(log/info :msg "WS Closed:" :reason reason-text))}})
There might be something to reference in this code: https://github.com/ptaoussanis/sente
@jdkealy You can access to the upgrade request via the websocket session object. Check out the Jetty websocket api for details.
Also, I’m trying to make the connection with some query parameters. I want to A:) make the websocket connection for the associated topic and B:) authenticate that the user has access to the topic
this came up before, i haven't tried it but the notes are here https://clojurians-log.clojureverse.org/pedestal/2020-04-09
thanks, it’s a bit over my head lol
is the hindol.adhya
suggesting to replace #(ws/add-ws-endpoints % ws-paths)
with ws-listener
? I’ll give that a try.
That's my snippet. The last line shows how to use it.
Ben has an alternative approach below that. But in both cases, you supply your own listener function to add-ws-endpoints.
ah cool i see… So you need to dip a bit into java. Between (.getHeaders _request)
/ (.getRequestURI _request)
I could hack getting the current user / request params. It would be super convenient to be able to leverage the interceptors, but this gets me what I need. Many thanks!