jaspervdj / websockets

A Haskell library for creating WebSocket-capable servers
http://jaspervdj.be/websockets
BSD 3-Clause "New" or "Revised" License
405 stars 112 forks source link

Proxy Producer from WebSocket? #40

Open jhickner opened 11 years ago

jhickner commented 11 years ago

Hello,

I have an existing application that uses the Pipes library, and I'm wondering if it's possible to create a Producer (source) from a websocket connection.

Usually creating producers is pretty simple. Here's an example that reads from a Chan:

chanReaderS :: Proxy p => Chan a -> () -> Producer p a IO ()
chanReaderS c () = runIdentityP . forever $ do
    v <- lift $ readChan c
    respond v

It's just a matter of looping forever trying to read from a resource, and then responding anything you get. But only being able to read from a websocket inside the WebSockets monad makes it tricky, since you can't use respond there.

One workaround I've found is to write to a Chan from inside the WebSockets monad, but then each client thread requires a second reader thread. I'm hoping there's a better way, but I haven't been able to find one. Any help would be appreciated.

Thanks

jaspervdj commented 11 years ago

This is indeed pretty hard at the moment. I hope to rewrite the WebSockets library using io-streams soon, once that's done this should become a lot more straightforward.

roman commented 10 years ago

@jhickner I guess what you are trying to do now should be possible with the 0.8 API