Git Product home page Git Product logo

Comments (4)

jarohen avatar jarohen commented on August 28, 2024

Yes - Chord supports this (in fact, it's the main use-case :) )

You can wrap the handler with the chord.http-kit/wrap-websocket-handler middleware, which puts a :ws-channel key into the request map. So your handler would look something like this:

(:require [chord.http-kit :refer [wrap-websocket-handler]])

(defroutes app-routes
  (GET "/ws" []
    (-> (fn [{:keys [ws-channel] :as req}]
          ;; do what you like with the WS here, e.g.: 
          (go-loop []
            (if-let [msg (a/<! ws-channel)]
              (handle-msg! msg)
              (handle-close!))))

        (wrap-websocket-handler))))

In your example above, it looks like you might need access to the websocket connection(s) from other routes, so you'll probably have to consider how your routes behave given certain websocket states (i.e. what happens if no-one's connected to the websocket, or what happens if more than one person is connected, etc). If you'd like help on how to structure this, let me know :) I've got an example application along these lines that I haven't yet open-sourced (but really should!), will post a link here.

James

from chord.

Gastove avatar Gastove commented on August 28, 2024

Oh man. I would love some guidance on how to structure something like
this -- websockets are a liiiittle new to me ;-P There's no rush, but if
you get around to posting a sample project here, I'd be much obliged.

Thanks!

On Thu, Dec 18, 2014 at 4:02 AM, James Henderson [email protected]
wrote:

Yes - Chord supports this (in fact, it's the main use-case :) )

You can wrap the handler with the chord.http-kit/wrap-websocket-handler
middleware, which puts a :ws-channel key into the request map. So your
handler would look something like this:

(:require [chord.http-kit :refer [wrap-websocket-handler]])

(defroutes app-routes
(GET "/ws" []
(-> (fn [{:keys [ws-channel] :as req}]
;; do what you like with the WS here, e.g.:
(go-loop [](if-let [msg %28a/<! ws-channel%29]
%28handle-msg! msg%29
%28handle-close!%29)))

    (wrap-websocket-handler))))

In your example above, it looks like you might need access to the
websocket connection(s) from other routes, so you'll probably have to
consider how your routes behave given certain websocket states (i.e. what
happens if no-one's connected to the websocket, or what happens if more
than one person is connected, etc). If you'd like help on how to structure
this, let me know :) I've got an example application along these lines that
I haven't yet open-sourced (but really should!), will post a link here.

James


Reply to this email directly or view it on GitHub
#25 (comment)
.

from chord.

Gastove avatar Gastove commented on August 28, 2024

(My inclination has been to store an identifier in the :session of a
given request, and use that to try and keep track of who-gets-what in the
multiple user case. Maybe there's a more sophisticated way to handle that,
though?)

On Thu, Dec 18, 2014 at 7:26 AM, Ross Donaldson [email protected] wrote:

Oh man. I would love some guidance on how to structure something like
this -- websockets are a liiiittle new to me ;-P There's no rush, but if
you get around to posting a sample project here, I'd be much obliged.

Thanks!

On Thu, Dec 18, 2014 at 4:02 AM, James Henderson <[email protected]

wrote:

Yes - Chord supports this (in fact, it's the main use-case :) )

You can wrap the handler with the chord.http-kit/wrap-websocket-handler
middleware, which puts a :ws-channel key into the request map. So your
handler would look something like this:

(:require [chord.http-kit :refer [wrap-websocket-handler]])

(defroutes app-routes
(GET "/ws" []
(-> (fn [{:keys [ws-channel] :as req}]
;; do what you like with the WS here, e.g.:
(go-loop [](if-let [msg %28a/<! ws-channel%29]
%28handle-msg! msg%29
%28handle-close!%29)))

    (wrap-websocket-handler))))

In your example above, it looks like you might need access to the
websocket connection(s) from other routes, so you'll probably have to
consider how your routes behave given certain websocket states (i.e. what
happens if no-one's connected to the websocket, or what happens if more
than one person is connected, etc). If you'd like help on how to structure
this, let me know :) I've got an example application along these lines that
I haven't yet open-sourced (but really should!), will post a link here.

James


Reply to this email directly or view it on GitHub
#25 (comment)
.

from chord.

jarohen avatar jarohen commented on August 28, 2024

Hi Ross - the example project repo is up at https://github.com/james-henderson/flow-chord-demo. I was meant to use it for a live demo for my talk at ClojureX but unfortunately couldn't find a good way to include it :/

The semantics here are that any number of clients can connect to the websocket route - each of them subscribes to a channel of updates that the routes close over. Other clients can then PUT to other HTTP routes, and any updates are echoed out to anyone who happens to be subscribed at the time. I'm making heavy use of core.async's 'mults' - splitting a channel so that many consumers can read from one channel.

Let me know if this is similar to your use case, or if you have any questions :)

James

from chord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.