Clojure and WebSockets - clojure

I'd like to use Clojure on the back-end to host a server via http-kit (serving the purpose of Node.js) and I would like to be able to have socket-based communication between server and clientside js. How can I achieve this functionality?

The sample code from the http-kit website works nicely:
(defn handler [request] (with-channel request channel
(on-close channel (fn [status] (println "client close it" status)))
(on-receive channel (fn [data] ;; echo it back
(send! channel data)))))
(run-server handler {:port 9090})
Put the run-server call inside a main function if you are not running from a REPL.
You can then access that websocket, just like you would do from nodejs.

Related

Clojure Pedestal Windows WSL2 browsers not rendering

I finished a pedestal clojure app and used the repl to start the server which it starts.
I can only curl and can't access the project on any browser with localhost:8080. I did the frontend with Clojurescript/Shadowcljs which works fine in the browser, so it can't be an overall issue via localhost (a host issue).
To further check issues I followed step by step this tutorial app (just to be 100% sure), and it too only loads via curl and not in the browser as the tutorial states it should be able to load in the browser (not changing any code).
I am using Windows wsl2 with all my apps of varies languages but this one with clojure and pedestal won't load in the browser no matter if I even try the dockerfile included.
Thank You for any help you can give me on this situation because I am lost.
So I was curios the way I got it to work was by adding> Thats using a browser on windows side to wsl linux using http://localhost:8080/
::http/host "0.0.0.0"
to
(def service {:env :prod
::http/host "0.0.0.0"
::http/routes routes
::http/resource-path "/public"
::http/type :jetty
::http/port 8080})
I found the info at
https://www.bleepingcomputer.com/news/security/wsl2-now-supports-localhost-connections-from-windows-10-apps/
https://github.com/pedestal/pedestal/issues/604#issuecomment-529469681

How to use `clj-http` connection pool from a ring web application?

I am using a clojure web application as a proxy web server.
All of my requests are coming into this clojure ring web application then I use clj-http to send the request on to the final destination.
So I have this working as a naive solution so far that simply calls clj-http/request for each request. This code is extremely similar to what I am doing.
But this isn't good enough yet because each time a request is made, a new http client is initialized. I need connection pooling so that http clients are re-used properly.
The clj-http documentation about persistent connections states that you re-use connections like this:
(with-connection-pool {:timeout 5 :threads 4 :insecure? false :default-per-route 10}
(get "http://example.org/1")
(post "http://example.org/2")
(get "http://example.org/3")
...
(get "http://example.org/999"))
Perhaps I am just not good enough with clojure yet, but how would someone surround all requests coming into https://github.com/tailrecursion/ring-proxy/blob/master/src/tailrecursion/ring_proxy.clj#L40 with this connection pool?
Implement a middleware that adds the connection manager into the request map.
You will need to handle the lifecycle of the connection manager yourself rather than the with- form - see the final part of the clj-http documentation on persistent connections.

Configure clojure http-kit to use a forward proxy

I have a little Clojure app that uses http-kit to send some http post requests to a server. I want to route the https POST request through a proxy P, ie. I want the traffic to go like App->Proxy->Server.
(This is because the target host X restricts access based on IP)
Is this possible?
Also the App runs on an ubuntu server, are there maybe system-level configurations possible to make http-kit use a proxy server? I prefer other processes to be unaffected though.
http-kit is supposed to follow the standard method of configuring proxies in Java:
-Dhttp.proxyHost=proxyhostURL \
-Dhttp.proxyPort=proxyPortNumber \
-Dhttp.proxyUser=someUserName \
-Dhttp.proxyPassword=somePassword
which you can set in your lein profile or in the application server if you are using one.
http-clj now support proxy: https://github.com/dakrone/clj-http#proxies
For http-kit, according to the author's reply in this issue, the answer is NO.
But the good news is fewer weeks before it support basic HTTP proxy ( commit a207537 on http-kit ).
After all, it seems there is no way to set up a system wide proxy for JVM applications.

add unique id to requests forwarded from nginx reverse proxy

We are running nginx as a reverse proxy that forwards requests to a Clojure application running Compojure, a library that wraps around Jetty and provides our application with the ability to service web requests.
We currently capture logs generated by both nginx and the Clojure application (via log4j to syslog). We are unable, however, to match an entry in the nginx log to an entry in the syslog output of the Clojure application.
We need to figure out a way to modify the request sent upstream to the Clojure app to include some kind of ID. This could be an integer, UUID, whatever.
Do you have any suggestions as to how best to accomplish this?
Thanks for your help!
Compojure is written on ring and ring has middleware :)
you would write a middleware called with-uuid that adds the UUID to the request map on the way in and to the reply on the way out.
Of course the best approach would be an nginx module, duplicating the functionality of apache's mod_unique_id.
There doesn't seem to be one yet. Here's a patch that wants to graduate to a module someday.:
http://mailman.nginx.org/pipermail/nginx-devel/2011-June/001015.html

How to implement asynchronous request response in REST based web service

I have a REST based web service system. I need to find a way to support publish/subscribe model here. As you know REST the communication between client and server is HTTP protocol. I use apache (PHP) web server in the backend to server all REST requests. The question is how to use PHP or whatever (in the web server side) to support this kind of Pub/Sub model. One typical scenario would be:
1) Client subscribes for a change in an object (GET /config/object/?type=async)
2) Client does not block with this request as it is async call.
3) Server accept the subscription and waits for the event.
4) Server publishes the client with the required data as and when the event happens.
I basically need to know how to implement all of these four steps above.
You are probably looking for something like PubSubHubbub -
http://code.google.com/apis/pubsubhubbub/
Letting PubSub implement the hub for you means you don't need blocking calls to the server.
They already have implemented example Subscribers and Publishers in different languages.
If haven't already, you should read Roy Fielding's take on the various approaches to PubSub. http://roy.gbiv.com/untangled/2008/paper-tigers-and-hidden-dragons