dfordivam / reflex-websocket-interface

A simple to use interface for websocket using reflex-frp
GNU General Public License v3.0
19 stars 4 forks source link

Async requests #9

Closed 3noch closed 6 years ago

3noch commented 6 years ago

It seems like websocket requests are synchronous (blocking). Is this true? If so, would it be possible to use async instead?

dfordivam commented 6 years ago

I guess you are talking about server side request handling...

I think it should be trivial to do asynch request handling.. (though I have not tested this yet).. simply forkIO whenever you receive a request..

There is no requirement at the client side code for the responses to come in any order..

3noch commented 6 years ago

Oh I see. So the websocket handler runs in a single thread for each unique client?

dfordivam commented 6 years ago

Well your server library has to do that, like yesod-websocket does it automatically for each client.

even for a single client you can fork a thread for each request...

dfordivam commented 6 years ago

like this

let loop = do
    d <- receiveData conn
    forkIO $ do
       print d
       resp <- handleRequest handler d
       print resp
       sendBinaryData conn resp
    loop

I think Network.WebSockets will have no problem with this..

dfordivam commented 6 years ago

Of course you cannot use StateT in this case.. but that' a separate issue.. you need to use TVar in ReaderT

3noch commented 6 years ago

Neat! Thank you.