HOW TO: Simple, Non-blocking Web Server in Clojure

4,951 views
4,563 views

Published on

Clojure meetup group presentation on non-blocking, asynchronous web servers in Clojure using Aleph. Super simple example of using Ring, Compojure, and Aleph to create a real time chat application.

Checkout the source on github: https://github.com/alexkehayias/clojure-aleph-chat

Published in: Technology, Design
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,951
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

HOW TO: Simple, Non-blocking Web Server in Clojure

  1. 1. HOW TO: Simple, Non-blocking Web Server in Clojure Alex Kehayias @alexkehayias
  2. 2. What do I want?• Non-blocking (async)• Simple• Fast• ConcurrentA framework for real-time web applications
  3. 3. Considerations• Python/Tornado• Javascript/Node
  4. 4. Enter Aleph• The only real option for Clojure• Built on Lamina – an abstraction for event driven programming• Fast (8.5k req/s in simple tests by David Nolen using a MBP)• Wraps your existing web stack (ring, compojure, noir, mustache, etc.)https://github.com/ztellman/aleph/
  5. 5. Real time chatSubscribe all user’s to the same channelwhen they are in the same location (i.e. url).Publish new messages created to allsubscribers.
  6. 6. DEMO
  7. 7. A simple server with routes(defn -main [&args](start-http-server (wrap-ring-handler app-routes) {:port 8080 :websocket true}))
  8. 8. Simple Chat View(defn chat [ch request] "View handler that handles a chat room. If its not a websocket request then return a rendered html response." (let [params (:route-params request) room (:room params)] (if (:websocket request) (chat-handler ch room) (enqueuech (wrapped-sync-app request)))))
  9. 9. Chat Handler(defn chat-init [ch] "Initialize a new chat channel" (receive-all ch #(println "message: " %)))(defn chat-handler [ch room] "Relays messages into a chat room. If it doesnt exist create a new channel" (let [chat (named-channel room chat-init)] (siphon chat ch) (siphonch chat)))
  10. 10. Usingweb-socket-js$ ->window.socket = new WebSocket window.location.href.replace("http://", "ws://")socket.onopen = ->console.log"socket opened"socket.onmessage= (msg) -> $("#messages").append("<p>"+msg.data+"</p>") $("form").on "submit", (e) ->e.preventDefault()socket.send$("#message").val() $("#message").val ""
  11. 11. Advantages• About 25 lines of code dedicated to aleph and websockets• Concurrent It’s Clojure, do something crazy.
  12. 12. Challenges• Documents need more detail for Lamina and Aleph API• Not many up-to-date examples (hopefully this project helps)• Socket.io not supported yet• Deploying it to a PaaS;; TODO• Close channels that aren’t in use• Store last n messages
  13. 13. Thanks!Get the source:https://github.com/alexkehayias/clojure-aleph-chatCheck out the blogpost:http://alexkehayias.tumblr.comSay hi!alex.kehayias@gmail.com@alexkehayias

×