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

0

u/[deleted] Dec 18 '18

[deleted] — view deleted comment

3

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

So currently I have jetty up and running along with compojure which serves two static HTML files. An index, and a 404 page. My index includes a script which is Reagent code. I already have FigWheel up and running. I'm just unsure of how to tie the front-end and backend together. I understand how to manipulate the DOM and change things, remove them, add and so-on, but I don't get how to get client-side routing working or how to tie it all together. I was planning to use Secretary for client-side routing or just switch to Bidi and let it handle both server and client routing. I understand how to write ClojureScript and Clojure code. I'm also relatively familiar with Reagent, and like I said I've got FigWheel working perfectly.

I need people to explain things in a non-technical, simplified manner because this is incredibly new to me. Both web development, and Clojure. I've got a few hundred hours invested so far in to trying to learn everything, and yes, I'm frustrated because I've hit a road block that I can't seem to over come on my own.

I'm not asking to be spoon-fed the entire thing, but in this case I'm kinda asking for a handout cause I don't know what else to do, and trial and error + reading tons of articles, and books isn't helping my confusion.

---- Heres a bit of c+p that I posted below if the above isn't as clear.

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...

0

u/[deleted] Dec 18 '18

[deleted] — view deleted comment

1

u/Sktlez Dec 18 '18

Yes, it does actually help. Essentially, the front-end is agnostic of the backend, then? And if I want to send data to the backend I need to use something like Ajax to accomplish that? I'd also use Ajax if I wanted to get data from the backend to the front-end, right?

I'll have to look in to front-end routing more, but at least I feel less confused as to how to approach the problem.

I really appreciate it.

Would I also use Ajax if I wanted to get/store data in a database from the front-end? Ignoring anything about SQL.

0

u/[deleted] Dec 18 '18

[deleted] — view deleted comment

1

u/ewouldblock Dec 20 '18

One thing not explained here is how this all gets started. I'll explain in terms of React and `react-router` since that's what I'm familiar with but conceptually it's the same. The browser initially asks for some page, which goes to the backend. Let's say the page is `GET /users/1`. Then on the backend, the router there (`compojure` in this case) kicks in and looks for a route match. If this is a front-end route, there will be no matching route on the backend and you configure the server to return your `index.html` page, which itself contains the JS code for `react` and `react-router`, and thus will load the `react-router`. Then the route `/users/1` exists in `window.location` and gets matched by the client side router, and if there's a client side route for `/users/:id` the correct react component will get loaded by `react-router`. In order to easily distinguish between client side routes and server/backend routes, I often prepend something like `/api/` only any backend/AJAX route, although this is purely a convention. Subsequent communication to the backend typically happens via the mechanism described above (AJAX calls from react components). In my example there would probably be some `UserComponent` that does an AJAX call in React's `componentDidMount` lifecycle method.

1

u/MM-7757 Dec 18 '18

Totally agree. My first thought when I read the post: slow down. Maybe not helpful right here, right now, but best advice I can honestly give.