Code Monkey home page Code Monkey logo

noir-async's Introduction

noir-async Build Status

Seamless, concise, async webservices for clojure.

noir-async integrates the noir web framework with the aleph async library with minimal syntax. With noir-async you can create well organized webapps, with both synchronous and asynchronous logic, and multiple asynchronous endpoints.

Getting Started

  1. Add [noir-async 1.1.0-beta7] to your project.clj
  2. Use the custom server.clj
  3. Follow the examples below
  4. Read the full API docs for more examples.

Here's an example route that responds in one shot:

; Note, same syntax as noir's defpage, but with "conn" parameter
(defpage-async "/route" [] conn
  (async-push conn {:status 404 :body "Couldn't find it!"}))

This is an example route that handles a websocket:

(defpage-async "/echo" [] conn
  (on-receive conn (fn [m] (async-push conn m))))

Here's an example of responding in a chunked fashion:

(defpage-async "/always-chunky" [] conn
  ;; Sending the header explicitly indicates a chunked response
  (async-push conn {:status 200 :chunked true})
  (async-push conn "chunk one")
  (async-push conn "chunk two")
  (close-connection conn))

Reading request bodies with aleph can be a little odd, so that interface is sugared:

(defpage-async "/some-route" {} conn
  ;; You can also retrieve the body as a ByteBuffer. See full docs for details
  (async-push (str "Received body:" (request-body-str conn))))

Since it uses an identical interface for both websockets and regular HTTP, if you want to handle them differently be sure to use the websocket? and regular? functions to discern them.

Server Setup

In your server.clj, you'll want to use aleph as a server explicitly. Be sure to replace noir-async-chat with the appropriate namespace.

(ns noir-async-chat.server
  (:use aleph.http
        noir.core
        lamina.core)
  (:require
    [noir.server :as nr-server] ))

(nr-server/load-views "src/noir_async_chat/views/")

(defn -main [& m]
  (let [mode (keyword (or (first m) :dev))
        port (Integer. (get (System/getenv) "PORT" "3000"))
        noir-handler (nr-server/gen-handler {:mode mode})]
    (start-http-server
      (wrap-ring-handler noir-handler)
      {:port port :websocket true})))

Full Docs

Full API Docs for more examples.

Apps Using noir-async

engulf noir-async-chat

License

Copyright (C) 2011 Andrew Cholakian

Distributed under the Eclipse Public License, the same as Clojure.

noir-async's People

Contributors

andrewvc avatar jeroenvandijk avatar

Stargazers

Angus H. avatar Th. Ma. avatar Itsuki Hayashi avatar Mark Nutter avatar Daniel Fagnan avatar  avatar  avatar Frank Siler avatar  avatar  avatar Ruiyun Wen avatar Jeffrey Konowitch avatar VMT avatar James Thornton avatar Isaka Traore avatar Lee Meichin avatar  avatar Andrew Whitehouse avatar  avatar  avatar Alex Kehayias avatar Ravi Kotecha avatar Matthew Todd avatar Dane Schneider avatar Hokuto Takai avatar Claudia Doppioslash avatar adamsxu avatar Daniel Woelfel avatar  avatar Aaron Steele avatar John Szakmeister avatar anton avatar James Greenhill avatar Fabien Bourgeois avatar Jens Bendisposto avatar Danny Wilson avatar Justin Tirrell avatar  avatar Fabian Bornhofen avatar Maksim Soltan avatar Eduardo Julián avatar Bryan Ivory avatar  avatar Reid Draper avatar FrankS avatar Carl Hörberg avatar David Steele avatar Peter Taoussanis avatar Rui Zhang avatar Andrey Vasenin avatar Igor P. Leroy avatar Levi Strope avatar Nicola Mometto avatar Kris Richardson avatar Paulo Suzart avatar  avatar  avatar Bob TheBuilder avatar Dmitri Sotnikov avatar Leandro Oliveira avatar Lambder avatar Jeremy Huffman avatar Ali Baitam avatar Chris McDevitt avatar Ed McCaffrey avatar Nathan Milford avatar  avatar Nils Grünwald avatar Matthew M. Nelson avatar Peter Weller avatar Steve Losh avatar Juliano Martinez avatar Karsten Schmidt avatar Tim Lopez avatar Paul Wisehart avatar  avatar Gleicon Moraes avatar Steffen Dienst avatar Zach Tellman avatar Clemens Oertel avatar  avatar Daniel Jomphe avatar Toby Hede avatar  avatar Josh Chaney avatar Alex Iwaniuk avatar Daniele Alessandri avatar  avatar Pedro Henriques dos Santos Teixeira avatar Paul Legato avatar Azer Koçulu avatar Sergey Pariev avatar manjilab avatar Rob Zinkov avatar Brian Schroeder avatar A. Amar avatar josh rotenberg avatar Michael Klishin avatar

Watchers

 avatar  avatar Kris Foster avatar Rui Zhang avatar FrankS avatar  avatar Daniel Woelfel avatar  avatar James Cloos avatar Andreas Kirkerud avatar  avatar

Forkers

jeroenvandijk

noir-async's Issues

error on (close conn)

Hi, I'm not so sure if this is really a bug (I still trying my first steps w/ clojure):

Stacktrace:

<server$start_server$fn__9025 aleph.netty.server$start_server$fn__9025@2a08904c>

clojure.lang.ArityException: Wrong number of args (1) passed to: core$async-push
at clojure.lang.AFn.throwArity(AFn.java:437)
at clojure.lang.AFn.invoke(AFn.java:39)
at noir_async.core$close.invoke(core.clj:85)
...

code: https://github.com/andrewvc/noir-async/blob/master/src/noir_async/core.clj#L85

it seems like there is no conn passed to the async-push when it yields an error. Not sure why I got this.

Offending code:

(defpage-async "/comet/:queue" {queue_name :queue} conn
(async-push conn {:status 200 :chunked true})
(async-push conn "chunk one")
(async-push conn "chunk two")
(async-push conn queue_name)
(close conn))

Thanks

the netty server is slow

I have try it, I benchmark for simple "hello word" page, it seems it much slower than jetty, and tornado(python).

is there something wrong or possible improvement?

async routes don't update changes in dev mode

Changes to the body of defpage-async aren't reflected in the app when running in dev mode.

If I define the following route:

(defpage-async "/async-route" {} conn
  (on-receive conn (fn [msg] (println "hello"))))

and then change it to

(defpage-async "/async-route" {} conn
  (on-receive conn (fn [msg] (println msg))))

I'll still see hello printed out instead of the message. Is there any way to automatically reload the routes on new connections?

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.