[web-devel] Handling disconnection in wai or warp

Michael Snoyman michael at snoyman.com
Mon Aug 29 12:07:23 CEST 2011


On Mon, Aug 29, 2011 at 1:03 PM, Hiromi ISHII <konn.jinro at gmail.com> wrote:
> Hi there,
>
> I am writing a simple server which returns infinite stream of data to client like Twitter's streaming API.
>
> I have to manage the number of clients and the connection times of each one.
> So I have to handle disconnections during server sending a response to the client.
>
> I'm currently considering using wai (+warp) or snap framework.
> My code fragments for snap and wai are below:
>
> ==== snap ver. application ====
> stream :: Application ()
> stream = do
>  req <- withRequest $ return . (rqRemoteAddr &&& rqRemotePort)
>  writeLBS $ LBS.unlines $ map (LBS.pack . show) [1..] -- I have to handle disconnection here
> ==== end ====
>
> ==== wai ver. ====
> streamHandler :: Application
> streamHandler req = do
>  return $ responseLBS statusOK
>                       [ ("Content-Type", "application/json")
>                       , ("Transfer-Encoding", "chunked")
>                       ]
>                       (LBS.unlines $ map (LBS.pack . show) [1..]) -- I have to handle disconnection here
> ==== end ====
>
>
> Thanks,
>
> -- Hiromi ISHII
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel
>

I'm not entirely certain what's going on here, but my guess is that
for either WAI or Snap you'd want to use the enumerator interface
instead of lazy ByteStrings. Also, for WAI, you should *not* set the
transfer-encoding, that is something the backend handles for you
automatically.

Michael



More information about the web-devel mailing list