Giter VIP home page Giter VIP logo

aleph's Introduction

Aleph is a Clojure framework for asynchronous communication, built on top of Netty and Lamina.

What is Aleph good for?

Aleph allows the creation of both clients and servers that can communicate using an array of protocols (HTTP, WebSockets, TCP, UDP, and others), and represents that communication via a single abstraction, channels. Thanks to the underlying libraries and the event-driven approach to communication, these clients and servers can be highly scalable.

Using Aleph in your project

In the project.clj file at the top level of your project, add Aleph as a dependency:

(defproject my-project "1.0.0"
  :dependencies [[org.clojure/clojure "1.5.0"]
                 [aleph "0.3.0-beta15"]])

Code examples

HTTP Server

Aleph conforms to the interface described by Ring, with one small difference: the request and response are decoupled.

(use 'lamina.core 'aleph.http)

(defn hello-world [channel request]
  (enqueue channel
    {:status 200
     :headers {"content-type" "text/html"}
     :body "Hello World!"}))

(start-http-server hello-world {:port 8008})

For more on HTTP functionality, read the wiki.

HTTP Client

This snippet prints out a never-ending sequence of tweets:

(use 'lamina.core 'aleph.http 'aleph.formats)

(let [ch (:body
           (sync-http-request
             {:method :get
              :basic-auth ["aleph_example" "_password"]
              :url "https://stream.twitter.com/1/statuses/sample.json"
              :delimiters ["\r"]}))]
  (doseq [tweet (map decode-json (lazy-channel-seq ch))]
    (prn tweet)))

A more in-depth exploration of this example can be found here.

WebSockets

Making a simple chat client is trivial. In this, we assume that the first message sent by the client is the user's name:

(use 'lamina.core 'aleph.http)

(def broadcast-channel (channel))

(defn chat-handler [ch handshake]
  (receive ch
    (fn [name]
      (siphon (map* #(str name ": " %) ch) broadcast-channel)
      (siphon broadcast-channel ch))))

(start-http-server chat-handler {:port 8008 :websocket true})

TCP Client/Server

Here is a basic echo server:

(use 'lamina.core 'aleph.tcp)

(defn echo-handler [channel client-info]
  (siphon channel channel))

(start-tcp-server echo-handler {:port 1234})

For more on TCP functionality, visit the wiki.

--

Other protocols are supported, and still more are forthcoming.

For more information, visit the wiki or the API documentation. If you have questions, please visit the mailing list.

aleph's People

Contributors

adhertz avatar brentonashworth avatar dajac avatar datskos avatar duck1123 avatar ivan avatar mhaemmerle avatar michaelklishin avatar mpenet avatar mshytikov avatar rje avatar rosejn avatar stuarthalloway avatar zane avatar ztellman avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.