Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:27:53+00:00 2026-06-03T13:27:53+00:00

I want to write a web server which stores its state in a State

  • 0

I want to write a web server which stores its state in a State monad with wai/warp. Something like this:

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.Wai.Handler.Warp
import Network.HTTP.Types
import Control.Monad.State
import Data.ByteString.Lazy.Char8

main = run 3000 app

text x = responseLBS
        status200
        [("Content-Type", "text/plain")]
    x

app req = return $ text "Hello World"

app1 req = modify (+1) >>= return . text . pack . show

-- main1 = runStateT (run 3000 app1) 0

The commented line doesn’t work, of course. The intent is to store a counter in a state monad and display its increasing value on every request.

Also, how do I get thread safety? Does warp run my middleware sequentially or in parallel?

What options are available for the state – is there anything at all besides IORef I can use in this scenario?

I understand that State gives safety but it seems wai doesn’t allow State.

I only need a dead-simple single-threaded RPC I can call from somewhere else. Haxr package requires a separate web server which is an overkill. See Calling Haskell from Node.JS – it didn’t have any suggestions so I wrote a simple server using Wai/Warp and Aeson. But it seems that WAI was designed to support concurrent implementatons so it complicates things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:27:54+00:00Added an answer on June 3, 2026 at 1:27 pm

    If your interaction with the state can be expressed with a single call to atomicModifyIORef, you can use that, and you don’t need to explicitly serialise access to the state.

    import Data.IORef
    
    main = do state <- newIORef 42
              run 3000 (app' state)
    
    app' :: IORef Int -> Application
    app' ref req
       = return . text . pack . show `liftM` atomicModifyIORef ref (\st -> (st + 1, st + 1))
    

    If your interaction is more complex and you need to enforce full serialisation of requests, you can use an MVar in conjunction with StateT.

    import Control.Concurrent.MVar
    import Control.Monad.State.Strict
    
    main = do state <- newMVar 42
              run 3000 (app' state)
    
    app' :: MVar Int -> Application
    app' ref request
       = do state <- takeMVar ref
            (response, newState) <- runStateT (application request) state
            putMVar newState --TODO: ensure putMVar happens even if an exception is thrown
            return response
    
    application :: Request -> StateT Int (ResourceT IO) Response
    application request = modify (+1) >>= return . text . pack . show
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a android application which needs data from the web. This
I already have a server with port and want to write a web app
I want to write a web site which can edit OpenOffice document ODF, user
I want to write web service for sparql enpoint so I need wsdl and
I want to write a web application and want to use Ruby. I have
I want to write a simple web framework myself using WSGI, Python. I am
I want to write a simple web proxy, for exercise. Here's the code I
I want to write some test cases for my web app, but stuck at
I want to test a Ajax based web application. I want to write the
I have created blank Asp.Net-MVC 3 web application and want to write my own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.