Comments (4)
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.
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.
(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.
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)
- AJAX call defaults to host/port of current page HOT 1
- How do I clean up after a client disconnects? HOT 3
- Close received after close HOT 13
- Running lein dev on the examples gives an exception HOT 1
- Clojure client? HOT 2
- Support immutant 2? HOT 2
- Example project - OutOfMemory error HOT 5
- Strange exception while having :optimizations :none in cljsbuild HOT 9
- Node.js HOT 3
- How do I use {:format :fression} on server side? (or: no method in multimethod for fression connection) HOT 3
- Messages appear on server as TaggedObject rather than clojure value. HOT 2
- Including chord triggers core.typed initialization error HOT 4
- WebSocket connection to 'ws://localhost:8080/streaming' failed: Invalid frame header HOT 5
- Server miss first message HOT 3
- Problem with routing compojure
- Remove dependencyon com.cemerick/urls HOT 1
- Serializing large message fails with {:format :edn} HOT 1
- Can't run with clojure 1.9.0 HOT 3
- Getting this error ``ERR_DISALLOWED_URL_SCHEME`` HOT 4
- transducers on write-ch receive formatted value HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chord.