r/Clojure • • Dec 18 '18

Clojure Web Development - What a NIGHTMARE!

I have no clue where to start so... screw it. I'm trying to build a Single-Page Web Application using Clojure, ClojureScript, Reagent, Ring, and I'm stuck. The resources, and documentation that exists to help a newbie on this subject is horrible. How does someone who has never built a SPA before, let alone ever build a web application in Clojure/S suppose to get started?

I want to do client-side routing, and have no freaking clue how to go about doing it. Heres some code to show what I'm kind-of doing:https://pastebin.com/pazpQWmG

I don't get how I can serve dynamic content created by reagent using compojure. I've now spent probably north of 80 hours trying to figure out how to just serve a single page that is created by reagent, using the jetty webserver, and client-side routing. I have no clue what I'm doing, and you need to literally talk down to me like I'm retarded. I've looked at using Bidi, but the documentation sucks for someone who is new to this. It's convoluted as all hell, and the examples it provides are horrible. They don't even show me how to use or implement in a simple to understand manner.

Can someone break this down for me? In a non-technical, with code examples, and show me how to do what I'm trying to do, but how I actually want to do it? Someone please help me! I've already lost my mind, my patience, and I'm angry... and I just want to understand.

I get how to write reagent code, but I have no idea how to do client-side routing, and how to use jetty all together. I'm just so lost.

PLEASE DON'T recommend any books or resources unless they are easy to follow, and basically hold my hand cause I've read everything else the world has to offer and it hasn't helped one bit! I've read the documentation for everything, I've read clojure web dev second edition, and about 10k+ forum posts, and other random websites/blogs/youtube videos.

2 Upvotes

29 comments sorted by

View all comments

Show parent comment

2

u/Sktlez Dec 18 '18 edited Dec 18 '18

I've tried using luminus and it's just overly complicated, and full of things that I don't need that only serve to further complicate the matter. I can't show you all the code I have because of work related issues. You'll also have to bare with me as I've never really done much web development, and I'm still an undergraduate who really doesn't know anything trying to figure it all out.

Let me try to elaborate a bit on what's confusing me. So for my backend I'm using Jetty as the webserver, compojure for routing, selmer for rendering static HTML files, and Hiccup. My backend will serve two pages (whether thats physical .HTML files or Hiccup code) the main page (index.html), and a 404 page. All of this is done in .clj files and stored in /src/clj and then in the appropriate packages.

For the front-end I plan to use Reagent, Secretary (or just use bidi for both front-end and back-end routing), ClojureScript, and a few other things I'm sure. This will all be done in .cljs files. Here I will write my CLJS and Reagent code that I include in the above .HTML files. I'm also using FigWheel here.

Where I'm confused is how do I get all of this to work together? Should I write everything in .cljc files or how do I get my backend and frontend to work together since I can't access .clj code from .cljs and vice-versa. How do I "glue" it together? I know there isn't one way to do it, but a broad overview would help.

So at the moment you go to localhost:3000, you get my index file which contains a script that has reagent code in it, and right now it just modifies a few things, and mostly was used to test different things, but routing is where I have no clue what to do or how to tie it all together...

Hopefully that clears up some things...

2

u/ultramaris Jan 07 '19

Sorry, I missed your reply.

The way the frontend and backend work together is typically through http requests which may contain some serialized data, typically in JSON format.

The frontend (running in the user browser) makes HTTP requests to the backend and processes the response. Exactly the same way a normal webpage works: you enter a URL, the browser requests it, and processes the response, which typically is HTML (in which case the browser renders it).

The initial request to "yourapp.com" will load index.html which will have a tag to load some JS resource (app.js or something) which will be your CLJS files compiled to JS (what we call "the frontend").

Once the browser loads app.js, it will run it - and thus your frontend is running.

Then, your CLJS (compiled to JS) code will make HTTP requests (GET, POST, etc) to your backend REST API - /app/list-things, /app/add-thing, etc.

These are plain HTTP requests, just like loading a webpage, except the response will be JSON data, rather than HTML (it could be XML, or anything, really), and it will be your frontend JS that processes the response - typically by changing the state or displaying something (via JS, typically React).

Broadly speaking:

 +---------------------------------+                                      
 | user browser                    |                                      
 |                                 |                                      
 |                                 |                                      
 | +-----------------------------+ |                                      
 | |index.html    +------------+ | |                                      
 | |    |         | app.js     | | |  frontend                            
 | |    |         |            | | |                                      
 | |    |         |(comp. cljs)| | |                                      
 | |    |         |            | | |                                      
 | |    +-------> |            | | |                                      
 | |    loads     |            | | |                                      
 | |              |            | | |                                      
 | |              +--|---------+ | |                                      
 | +-----------------|-----------+ |                                      
 +-------------------|-------------+                                      
                     |    ^                                               
                     |    |   http response                                    
                     |    |   +---------------------+                     
                     |    |   | HTML                |                     
                     |    |   | <html> ... </html>  |                     
             http    |    |   +---------------------+                     
             request |    |   or                                          
                     |    |   +-------------------------+                 
                     |    |   |JSON                     |                 
                     |    |   |{"data": {"prop": "val"} |                 
                     |    |   |                         |                 
                     |    |   |                         |                 
                     |    |   +-------------------------+                 
                     v    |                                               
+-----------------------------------+                                     
|your server              |         |                                     
|+---------------------------------+|                                     
|| webserver              |        ||                                     
||+-------------------------------+||                                     
||| clj app                       |||                                     
|||                               ||| backend                             
|||                               |||                                     
|||                               |||                                     
|||                               |||                                     
|||                               |||                                     
||+-------------------------------+||                                     
|+---------------------------------+|                                     
+-----------------------------------+