Ring Clojure Documentation

repository·master·Indexed 26 days ago

https://github.com/ring-clojure/ring

A Clojure web applications library inspired by Python's WSGI and Ruby's Rack. Ring abstracts HTTP details into a unified API for modular web components, providing core functions, middleware, and adapters for servers like Jetty. It supports synchronous and asynchronous handlers, WebSocket responses via ring.websocket.protocols, and integration with Java and Jakarta Servlets.

Tokens
1.1K
Snippets
5
Records
12
Agent score
37%

What's inside Ring

  1. Overview of Ring libraries

    master

    Ring provides several specialized libraries depending on your needs:

    • ring/ring: A meta-package containing all relevant dependencies.
    • ring/ring-core: Core functions and middleware for Ring handlers, requests, and responses.
    • org.ring-clojure/ring-core-protocols: Protocols necessary for building Ring responses.
    • org.ring-clojure/ring-websocket-protocols: Protocols necessary for WebSockets.
    • ring/ring-devel: Functions for developing and debugging Ring applications.
    • ring/ring-servlet: Construct legacy Java Servlets (≤ 4.0) from Ring handlers.
    • org.ring-clojure/ring-jakarta-servlet: Construct Jakarta Servlets (≥ 5.0) from Ring handlers.
    • ring/ring-jetty-adapter: An adapter that uses an embedded Jetty web server.
  2. Implement an Asynchronous Ring Handler

    master

    An asynchronous handler takes three arguments: a request map, a respond callback function (which takes a response map), and a raise callback function (which takes an exception). The return value is ignored.

    A single function can support both synchronous and asynchronous arities.

    ;; Asynchronous arity
    (fn [request respond raise]
      (respond response))
    
    (fn [request respond raise]
      (raise exception))
    
    ;; Dual-purpose arity
    (fn
      ([request]
        response)
      ([request respond raise]
        (respond response)))
  3. Implement Websocket Responses

    master

    To promote an HTTP request to a websocket, a handler can return a websocket response map instead of a standard HTTP response map. This can be done in both synchronous and asynchronous handlers.

    ;; Synchronous
    (fn [request]
      #:ring.websocket{:listener websocket-listener})
    
    ;; Asynchronous
    (fn [request respond raise]
      (respond #:ring.websocket{:listener websocket-listener}))
  4. Implement Middleware in Ring

    master
    Middleware are higher-order functions used to augment handler functionality. They take one or more handlers and configuration options as arguments and return a new handler with the additional behavior.
  5. Run an Adapter to start an HTTP server

    master

    Adapters are side-effectful functions that start an HTTP server. They take a handler and a map of options. The adapter parses incoming HTTP requests into request maps and passes them to the handler, then uses the returned response map to send an HTTP response to the client.

    (run-adapter handler options)
  6. Implement the ring.websocket.protocols/Listener protocol

    master

    A websocket listener handles lifecycle and message events. It must implement the following methods:

    • on-open [listener socket]: Called when the websocket is successfully opened.
    • on-message [listener socket message]: Called when a text or binary message is received. message is a java.lang.CharSequence or java.nio.ByteBuffer.
    • on-pong [listener socket data]: Called when a pong frame is received.
    • on-error [listener socket throwable]: Called when an error occurs.
    • on-close [listener socket code reason]: Called when the websocket is closed. Guaranteed to be called if on-open was called.
  7. Implement the ring.websocket.protocols/Socket protocol

    master

    A socket object represents the active connection and provides methods to interact with the client:

    • -open? [socket]: Returns truthy if the socket is connected.
    • -send [socket message]: Sends a text (CharSequence) or binary (ByteBuffer) message.
    • -ping [socket data]: Sends a ping frame.
    • -pong [socket data]: Sends an unsolicited pong frame.
    • -close [socket code reason]: Closes the connection with a code and reason.