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

  • 3,280 views
Uploaded 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. …

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

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,280
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
8
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. HOW TO: Simple, Non-blocking Web Server in Clojure Alex Kehayias @alexkehayias
  • 2. What do I want?• Non-blocking (async)• Simple• Fast• ConcurrentA framework for real-time web applications
  • 3. Considerations• Python/Tornado• Javascript/Node
  • 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. 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. DEMO
  • 7. A simple server with routes(defn -main [&args](start-http-server (wrap-ring-handler app-routes) {:port 8080 :websocket true}))
  • 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. 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. 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. Advantages• About 25 lines of code dedicated to aleph and websockets• Concurrent It’s Clojure, do something crazy.
  • 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. Thanks!Get the source:https://github.com/alexkehayias/clojure-aleph-chatCheck out the blogpost:http://alexkehayias.tumblr.comSay hi!alex.kehayias@gmail.com@alexkehayias