Fork me on GitHub
Lucas Barbosa01:10:02

I am trying to think about how to build this small system, just for fun/learning purposes, and I’d like to hear some advise from you guys. I want to build a chat server in Clojure. The idea is: clients connect (tcp socket) to the server and receive a prompt for either A: create a new room or B: connect to an existing room. After entering a room (either newly created or existing one), you can broadcast messages there and receive messages from other people on the same room. I would like to see events too, like “John disconnected” or “Paul connected”. Since the sole purpose of this is learning, I want to avoid using third party libraries. I want plain old Java ServerSocket and Socket. Would you give me architectural ideas? I guess this is a good scenario for core.async, but I am having a bad time thinking about how to structure the application

Lucas Barbosa01:10:11

All advise is welcome


@lvbarbosa what about an atom with room names as keys and socket connections as values?


then, for each message sent to a room, you iterate and send it to each socket under that value


after that, it's just a question of listening on each connected socket (which is of course reverse-mapped to room also)

Lucas Barbosa01:10:51

How would I detect a new message? This is something I can’t figure out

Lucas Barbosa01:10:00

Should I have a thread listening for each socket?

Lucas Barbosa01:10:20

or maybe worker threads scanning each socket for a bit of time and then going to the next one


the trivial thing (that doesn't scale but works for the first hundreds of clients) is a thread created with future, one for each socket connection


if you need more than a thousand or so clients, then you get into nio nonblocking stuff and maybe core.async for coordination

Lucas Barbosa01:10:09

I guess I’ll go with the simpler solution for now


you don't need to scan, each thread can do a blocking read on a socket


yeah - in my experience doing the simple thing that works first is easier than trying to optimize first and then realizing you need a completely different optimization

Lucas Barbosa01:10:48

but wouldn’t that require a single thread per connection?


right, and up to the first 1000 connections or so that just works


no need to get fancy

Lucas Barbosa02:10:43

After that, in case I want to improve it, I should seek java.nio for async stuff?


the OS knows it can't wake your thread up until input is ready, so it's not like perf is super bad (though of course nio would perform better)


nio of the io part, core.async for the coordination of the async results (if needed) yeah

Lucas Barbosa02:10:54

awesome! I will try the simple alternative for now 😄

Lucas Barbosa02:10:05

I’ll share it here as soon as I have something working


also, aleph makes a lot of this async network stuff easier in clojure


(though I realize you did say you wanted to use the raw java libs)

Lucas Barbosa02:10:07

I will take a look into that, maybe I get some ideas

Lucas Barbosa02:10:21

Yeah, I also want to learn network communication

Lucas Barbosa02:10:36

I know the theory, but I don’t have much practice


the idea with aleph (and it's support lib manifold) is to leverage ztellman's experience and provide a bunch of the little stuff you don't get by default but you will really want if performance starts to matter

Lucas Barbosa02:10:38

note taken! I’m sure it will be useful later


I have a question about om I want to update the app-state but I am failing to find the correct way to do it. Can someone please point me in the correct direction?


I would suggest looking up the tutorial for Falcor, which is part of the inspiration for Om. I had a bit of trouble understanding Om back when I first started, and found the Falcor tutorials much later, and was like, "Wow, wish I'd seen this sooner"


awesome, thanks mate. I ended up coding all night to get the new version ready and I think I have fallen in love with Clojurescript in the same way I hate Javascript.


any idea why this fails to match? (let [a 1] (case (class a) java.lang.String a java.lang.Integer (str a) java.lang.Long (str a) java.lang.Double (str a)))


it's because the test constants in case are not evaluated


boot.user=> (case 'java.lang.Long
       #_=>   java.lang.Long 2)


IllegalArgumentException No matching clause: class java.lang.Long


using (condp = (class a) was there a breaking change some time ago as I'm working thought an old example on the prag prog blog


I can't help you but I couldn't help smiling at that problem, giving that prag prog had a chapter on "select isn't broken" 😛


what do you mean select isn't broken?


I’m doing a clojure application but one thing bother me. I don’t know where/when should I close the version ? I’m still with snapshot, how you guys normally does that ?


Trimming off the "-SNAPSHOT" is usually the last thing I do before bundling the app up as an official release. Edit the project.clj, build the app without the "-SNAPSHOT", and ship it. Then I usually edit project.clj, bump the version number, and re-add "-SNAPSHOT"


Caveat: my personal experience shipping one in-house app. 🙂


I'm trying to convert a function that takes a callback to use a channel


(defn doThingCh [] (let [c (async/chan)]
      (fn [res]
                 (println "putting c" res)
                 (async/put! c res)
                 (println "close c")
                 (async/close! c))


...and as I'm typing the rest of my question I figured it out 😆


I was taking it off the channel and expecting a field to live on the response that doesn't exist


Anyone know the shortcut to eval-form-at-point in nightcode? I only use nightcode occasionally and I'm pretty certain it had that shortcut last time.


I am performing a bunch of isolated tasks in an async manner.

(defn google-analytics [channel]
    (Thread/sleep 1000)
    (>! channel :google-analytics)))

(defn facebook [channel]
    (>! channel :facebook)))i

(defn run []
  (let [pipeline (chan 2)]
    (google-analytics pipeline)
    (facebook pipeline)

    (go-loop []
      (let [message (<! pipeline)]
        (println "Got:" message))
This code works as is but it leaves the channel "pending". I'm looking for the best way to detect whether I have collected all items that I care about from the channel so I can close! and log it. Ideas?


@rinaldi read from the two channels returned by the go blocks, then close the channel?


(in another go block, of course)


I am not sure how to do that


(let [pipeline (chan 2)
    fb (facebook pipeline)
    ga (google-analytics pipeline)]
  (go (<! fb)
      (<! ga)
      (close! pipeline))


Hey all, sorry in advance, this question is long. Before I dive into a larger question, I was looking for an idiomatic way to findIndex on a list of maps.

(books ({id: 23} {id: 45}))
and noticed that findIndex is not part of the core library. This lead me to read through a section in Programming Clojure about choosing the correct data structure. To summarize, the idea seems to be that if you are going to be searching for an item, for example, using a function like findIndex (JS) against some data structure, you should be using a set. The idea is to communicate intent. This is why, to my understanding, findIndex is not part of the core library. What happens though when you are provided with a list, with a large dataset, and you need to use something like findIndex on it? Eventually, with large enough data, this becomes a performance issue point. Which goes against idiomatic clojure - do clojurists just sigh and move on, or is there a better way to handle this kind of situation? For example, you are writing clojurescript, and receive a a JS array of objects...this would become a list (as far as I know) . To loop back to the original question - what does idiomatic findIndex look like. Here is what I did to implement find like functionality:
(first (filter #(= (.. % -id) 823423) js/comments))


Regardless of whether you have an array (Java/JS) or a list, a linear search for a matching item is always going to be O(n) in any language.


^ @tkjone Generally, in Clojure(Script), if you want fast lookup by a unique ID, you just use a hash map instead: (books {23 {:id 23 :title "..."} 45 {:id 45 :title "..."}})


You might be given an array, but if the id is unique, you can easily turn that into a hash map accessed by that value.


(reduce (fn [m v] (assoc m (:id v) v)) {} book-list) for example


(or, if you want to be a bit more "modern" about that: (into {} (map (juxt :id identity)) book-list) 🙂 )


Excellent. So the answer to the fundamental question: transform the data structure you are provided into another data structure.


In this vein, if you are presented with a task where the ask is to find the index of the book with id: 45 on this data structure:

const books = [
  { id: 32 },
  { id: 34 },
  { id: 45 }
  // ...
(apologies for the JS) The idiomatic way to handle this in clojure would be to transform the object into a hash map (given that the ID's are unique) and thus, you would never have to make a findIndex function.


Right. Because findIndex is O(n) and slow if you are going to be repeating it.


My question would be: what would you do with that index? Are you just trying to find the item (and you don't care about the collection after that)?


ha - good call. My next question was going to be whether or not our proposal of the idiomatic way would be handled differently based on what the index is being used for - our ultimate goal that is.


If it really is just "find the item" (and ignore the rest), it's fairly idiomatic to say (first (filter (fn [v] (= (:id v) ??)) book-list)) -- that's not entirely efficient but...


(where ?? would be whatever ID you're searching for)


(remember in Clojure you don't update things in place because we're immutable 🙂 )


True. I imagine if I had a real world scenario I could answer this question a little better, but this is more a test problem. for example, the question was: implement findIndex in clojure.


But when I researched a little, this seems to be an odd ask in clojureland...where as in JS land, its a method on Array


so I am working on implementing like this:

     (fn [index item]
         (when (= (.. item -id) 45)
              index)) js/books)
just to show how it would be implemented, but if I was actually answering this question, I would say we would need to reframe the question.

Lucas Barbosa23:10:18

Can someone explain to me how pmap works in plain english?

Lucas Barbosa23:10:27

I can’t understand the docs

Lucas Barbosa23:10:38

I understand the difference from it’s cousin map in that it runs in parallel, but what is running in parallel?


it’s calling f in parallel, but not on all the args at once, it only runs it a specific number of threads, determined by how many CPUs you have


in order to maximize throughput


if you access the first item it returns, it will call a certain number of threads and realize those - but it is lazy in the sense that you need to consume all the results to ensure they all ran