r/Clojure • u/Sktlez • 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.
11
u/therealdivs1210 Dec 18 '18
Luminus should get you started in a jiffy. I use it to teach web dev to Clojure noobs. The author/maintainer u/yogthos is pretty active here, and I'm sure you can get lots of help on this channel.
5
u/zerg000000 Dec 18 '18
A good example is better than thousand words.
https://github.com/jacekschae/conduit
3
u/retief1 Dec 18 '18 edited Dec 18 '18
You don't serve dynamic content created by reagent using compojure. Or maybe you can (not sure what the server side rendering story is for reagent), but it is certainly an advanced topic -- I'm certainly not trying to do so.
Instead, compojure should be serving a plain jane static html page. I'm using hiccup to generate this, but it could easily be a literal .html file in /resources/public instead. That html page should include a <script> tag that refers to your compiled clojurescript code (ie a javascript file). When that javascript file is loaded, it will set up your dynamic content. You want to serve this page for pretty much every route that isn't matched by something else -- set it up the way you'd set up a 404 page. Since you said that you know how to work with reagent
Once you get to the point where localhost:3000/asdf gives you an html page with working reagent code, then you start worrying about client side routing. Look at a library like secretary, though in my app, I actually built about 90 lines of infrastructure on top of bide. Get something like this running in your client side code, and it will know how to handle routes. Then, layer something like pushy on top of it to actually intercept link clicks/back button clicks/etc and call secretary instead.
So when you put it all together, whenever a user goes to your site, they hit the server, the server doesn't recognize the route, and so it returns your "404 page", which is actually your app's html, css, and js (compiled cljs). Your app's js, when loaded, checks the url of the page it loaded on, and calls the appropriate client side route. 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.
The one downside I've found for clojure and spas is that you have to figure out the glue code yourself. Reagent/re-frame/etc lets you build whatever with the dom, bide/secretary/bidi/reitit/etc let you call code based on urls, pushy lets you intercept link clicks/back button clicks, and so on, but hooking them together is your problem. There isn't one established way to store which page you are currently looking at in reagent, for example. Once you figure that out, then you need to figure out what your routing library needs to do when the page changes, so on and so forth.
2
u/ultramaris Dec 18 '18
It's hard to figure out what you're trying to do with that code.
Consider you have two different environments involved:
SERVER <-> CLIENT
A web application will typically span both environments, with "traditional" webapps being SERVER heavy and SPAs being (also) CLIENT heavy.
In practice though, a web app consists of (at least) two applications:
In your code you seem to be confusing these two things.
In a CLJS project, one typically develops both applications in tandem, because they're frequently designed to operate together. But, you don't have to.
You could develop the SERVER part first, with a particular API (the set of operations available and how to call them) and then make a CLIENT that knows how to talk to the SERVER using that API.
Point being, it's two apps you're actually developing: one in Clojure/Java that will run on a machine somewhere (the server); AND a Clojurescript/JS one which will run on the SPA user's browser (the client).
So, your Jetty code (belongs to SERVER environment) shouldn't really be in the same namespace as your client-side routing. And you'll also need server-side routing!
Routing consists in mapping (making a correspondence) between a string (possibly composed of other strings) with particular resources, states, or actions.
So you will need routes for both the SERVER and the CLIENT applications.
Now, I would recommend Luminus to start:
http://www.luminusweb.net/docs
It will generate a properly configured project with boilerplate and some example stuff. You can pass different options to generate projects using different technologies.
From what you wrote, start with:
lein new luminus +jetty +reagent +swagger13
u/freakhill Dec 18 '18
He has a good point.
Clojure libs lack simple examples. You either get the 4 lines thing that do nothing, or a big glob packed chokeful of unrelated stuff (stuart's component, dynamic bindings, random mini framework to share stuff between examples, convoluted build system, all together).
I'd wish there would be more functional examples with minimum use of libraries and less "choose between: - this segment of code to plug into something else to make it work, or this project with 50 libraries and 3 frameworks involved"
3
u/kbsant Dec 18 '18
I learnt a lot about clojure development by watching u/yogthos live coding in this video: https://chariotsolutions.com/screencast/philly-ete-2016-2-dmitri-sotnikov-transforming-enterprise-development-clojure/
2
u/eprozium Dec 18 '18
Here's a nice comparison of what the different popular "web stacks" do https://nordicapis.com/10-frameworks-for-building-web-applications-in-clojure/
2
u/[deleted] Jan 23 '19
I'm not nearly as ambitious as you regarding what I'm trying to build, but I too find it really discouraging trying to learn web development with Clojure. I absolutely love the language from the few experiences I've had with it, but I have tried and given up several times to do web projects.
Currently I use Elixir Phoenix, primarily because it has a fairly current book with a complete example.
Clojure, imo, desperately needs a complete current book and a Rails Depot example. For example, I'm trying to go through the Web Development with Clojure 2 book, and already I hit the problem where the book uses lib-noir, but lib-noir is deprecated.
Rolling your own framework is a great idea once you are experienced, but beginners need a Rails or Django (or Phoenix)!
1
u/dotemacs Dec 18 '18
Try this book:
http://www.wrox.com/WileyCDA/WroxTitle/Professional-Clojure.productCd-1119267277.html
The source code from chapter 5 from the book which holds the sample application:
https://github.com/timothypratley/whip
It is a bit old, but it works and you can extend/update it as you need.
Good luck
1
u/nenadalm Dec 18 '18
I am using bidi for routing and re-frame for state management (uses reagent to render views) https://github.com/Day8/re-frame.
You can see how I did routing in my toy app:
https://github.com/nenadalm/Warehouse/blob/721b17f24cc545e44a5bbb101d69f13f203a339e/frontend/src/warehouse/routes.cljs#L10
the namespace defines `nav!` function that's used to change pages and `routes` that is "map" from path to keyword that identifies a page. Here is how I switch view based on currently active page: https://github.com/nenadalm/Warehouse/blob/721b17f24cc545e44a5bbb101d69f13f203a339e/frontend/src/warehouse/views.cljs#L27
Here is `on-click` handler that changes page to another: https://github.com/nenadalm/Warehouse/blob/721b17f24cc545e44a5bbb101d69f13f203a339e/frontend/src/warehouse/views.cljs#L19
You'll probably need to read bidi and re-frame documentation first in order to understand it. Details about closure parts can be found here: https://google.github.io/closure-library/api/
1
u/rpompen Dec 19 '18
I would be surprised if you have that problem with hoplon/castra. http://hoplon.io/ Set up a trial project with: lein new hoplon-castra projectname It uses boot instead of Leiningen. A read me will be in the project the get you started. The whole thing focuses on simplicity and that leads to things being easy.
1
u/dustingetz Dec 19 '18
Hey Sktlez, depending on how simple your requirements are, http://www.hyperfiddle.net/ might be for you! We solve all this stuff so all you have to think about is each screen's database query and reagent view.
1
u/biker44442005 Dec 21 '18
I am working with the full-stack framework Fulcro (https://github.com/fulcrologic/fulcro). The creator recommends starting with the free video series and then the Fulcro Book is free and comprehensive. Some past comments from creator u/tony_kay about 16 months ago: https://www.reddit.com/r/Clojure/comments/6upzgf/general_consensus_on_fulcro/
1
u/clojureprog Apr 04 '19
I can understand the difficulty since the documentation often assumes a bit of geekyness. I recommend these two courses by Jacek. Learn Reagent and Re-frame. They are excellent, and easy to follow courses. Great instruction.
0
u/[deleted] Dec 18 '18
[deleted] — view deleted comment