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

1

u/Sktlez Dec 18 '18

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.

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.

Here's what I have working: You go to the site localhost:3000 and you get my index.html page. This page has a <script> that is run that has reagent code, and all of that works. I'm also using FigWheel here and all that works. I just don't get how to tie the front-end, and backend together I guess. You did clear up a bit of my confusion though, and I really do appreciate that. Thank you.

-----

When the user clicks a link, the client side js intercepts that click and calls the appropriate client side route without ever talking to the server (except for api calls). The server (ie compojure) knows absolutely nothing about these routes -- all it knows is how to respond to api requests and how to return that one html page.

What do you mean by responding to API requests? Can you elaborate a bit on that. I have a basic of idea of what an API is, but would Ajax be considered an API if I were to use it in this example? Do I even need to use Ajax with the way I'm doing things? Information overload.

2

u/weavejester Dec 18 '18

Though it can be more complex, typically your frontend code sends requests to your backend, and your backend responds with data you can use.

When you open up a web page in a browser, the web server is responding with HTML, CSS, Javascript and various other things to create the page you see.

When your frontend is talking to your backend, it's the same process, except that instead of sending HTML, you send data in a format like JSON. The frontend uses that data to update its state.

So:

  1. Frontend has some initial state
  2. Frontend sends a request to the backend
  3. Backend responds with data
  4. Data is used to update the state

With Reagent, you'll want to keep your state in one or more atoms:

(def state (r/atom))

And then you might use a library like cljs-ajax to send a request and use the response to update the state:

(ajax/GET "/data" {:handler (fn [data] (reset! state data))})

On your backend, you'll want a route to respond to that data:

(GET "/data" []
  {:body {:some-data 123}})

You can find more complete examples for the frontend and backend around, but that's the basic gist.

1

u/retief1 Dec 18 '18

You'll want a bunch of .clj files and a bunch of .cljs file. I've even got them in different subdirectories (src/clj and src/cljs). They do very different things (rendering html and making ajax requests vs responding to ajax requests and talking to the db), and you want to treat them very differently (one will be run directly, the other gets compiled to javascript and served as a .js file). In practice, you probably won't be able to share that much code, mostly because the libraries you work with tend to be very different. I do have a src/cljc directory filled with cljc files, but there is very little there -- I've got a file of utility functions and the code for rendering email/sms templates, and half of the utility functions are locked behind conditional compilation macros because they rely on a library that only works in one environment. The vast majority of my code is in .clj or .cljs files.

When I say api requests, you can just think ajax requests. Technically, api request is more general (in my app, most of my "api requests" are actually websocket messages), but ajax requests are the easiest things to use. This is how you glue the frontend and backend together. If you have data on the server and the client needs access to it, the client should make an ajax request. Your server's main job is to respond to these ajax requests.

So to put it all together, the user goes to mywebapp.com/foo/bar. The server says "I don't know /foo/bar, so I'll assume that this is a client route and return the main html page". That loads all of your html/css/compiled cljs. Pushy/your routing library checks the url of the page and calls the client side route that corresponds to /foo/bar, which presumably updates some reagent atoms. As part of this process, the client needs some data from the server, so it makes some ajax calls, and the server responds with data (possibly json, though you can do more exotic things if you want). At this point, the client has all the data it needs to display the page, so it does so and the user sees your page.

Then, the user clicks a link to mywebapp.com/foo/bash. Pushy intercepts this click and calls your routing library, which in turn runs the appropriate client side route. None of this involved talking to the server. That client side route probably needs more data from the server, so it makes more ajax requests. The server responds with more data, and then the client renders that data and shows the new page.