Fork me on GitHub
#pedestal
<
2020-05-24
>
jdkealy20:05:40

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))}})

adam62214:05:33

There might be something to reference in this code: https://github.com/ptaoussanis/sente

ddeaguiar21:05:54

@ You can access to the upgrade request via the websocket session object. Check out the Jetty websocket api for details.

jdkealy20:05:47

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

danvingo21:05:26

this came up before, i haven't tried it but the notes are here https://clojurians-log.clojureverse.org/pedestal/2020-04-09

jdkealy21:05:42

thanks, it’s a bit over my head lol

jdkealy21:05:35

is the hindol.adhya suggesting to replace #(ws/add-ws-endpoints % ws-paths) with ws-listener ? I’ll give that a try.

hindol.adhya21:05:42

That's my snippet. The last line shows how to use it.

hindol.adhya21:05:04

Ben has an alternative approach below that. But in both cases, you supply your own listener function to add-ws-endpoints.

jdkealy22:05:27

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!