HOW TO: Simple, Non-blocking Web Server in Clojure
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 4,021 views

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

Statistics

Views

Total Views
4,021
Views on SlideShare
3,986
Embed Views
35

Actions

Likes
1
Downloads
7
Comments
0

1 Embed 35

https://twitter.com 35

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

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