Hack sample- chat server

Not that you’ll want to replace IRC with this any time soon, but I’ve put together an incredibly simplistic chat server to demonstrate Hack. The code is available in my hack-samples github repo. I’m not going to copy the source code here, but point out a few cool pieces.

In the imports list, I say import qualified Hack.Handler.SimpleServer as Handler to use the SimpleServer handler. Later on, I use Handler.run 3000 to run a simple server on port 3000. If you instead replace SimpleServer with CGI and remove the 3000, you immediately have a CGI application. Of course, all of the MVar concurrency code is unneeded overhead in a CGI application, but you could also use Happstack, FastCGI or any other handler.

Also, I alluded in my previous post to the idea of using currying to initiate some stuff. This code is a perfect example. In my main function, I load up data from a text file, create a Handle to write to, wrap them both in MVars and use that to curry the app function. This way, all of that initialization code only gets called once, no matter how many requests are served. This is a simple approach which works very well in production.

Not much more to say, I think the code speaks for itself!


3 Responses to “Hack sample- chat server”

  1. Haskell and chat « absolute regularity Says:

    […] Hack sample — chat server […]

  2. Teague Says:

    Hi Michael. I just found Hack and it looks incredibly cool. I can’t wait to dig more in to it. Thanks!

    In you code I noticed you asked about strictLines not being a pure function. I’m not sure if you solved this or not yet, but if you lift strictLines into the IO Monad then it works just fine.

    After changing line 34 to:
    then strictLines `liftM` U.readFile logFile
    and lines 47 & 48 to:
    strictLines :: String -> [String]
    strictLines s = length s `seq` lines s
    it works just fine. You also need to import Control.Monad (liftM).

    Thanks again, and I look forward to seeing what you come up with next.

  3. Teague Says:

    OK on second thought, I see the problem is more complex. Sorry about that; it’s late 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: