Client support for server-sent events (SSE)#1317
Client support for server-sent events (SSE)#1317vapourismo wants to merge 3 commits intohaskell-servant:masterfrom vapourismo:feature/server-sent-events
Conversation
|
Ping @haskell-servant/maintainers |
|
Thanks for the PR / sorry for the low bandwidth! I do not speak for all maintainers, but I think our priorities are #1312, then #1314, then release, then see what's up next, so it may take a while before this gets the attention it deserves. Is there any reason why this should go to Other maintainers: any thoughts? @vapourismo would you be open to joining us? |
|
Thanks for the update. I don’t need this merged right away - so no pressure. |
|
I don't see anything blocking this. Why has this not moved forward? I rebased this branch against master here: https://github.com/hasufell/servant/tree/feature/server-sent-events |
|
Yes, ping @haskell-servant/maintainers on this. I personally don't see anything shocking. |
|
Hey Ole, I am running into some trouble trying to use your great work here and would appreciate it if you can point me in the right direction. My server: I have a server running with an SSE endpoint that spits back strings of My Here's my client main function: main :: IO ()
main = do
mgr <- newTlsManager
let env = mkClientEnv mgr $ BaseUrl Http "localhost" 8000 ""
withClientM cli env $ \case
Left err -> (print err)
Right stream -> do
putStrLn "operating on stream obj"
let sourceT = unJsonEventStreamT stream
S.unSourceT sourceT go
where
go :: StepT IO (Event DummyObj) -> IO ()
go S.Stop = return ()
go (S.Error err) = do
print "error"
print err
go (S.Skip s) = do
print "skip"
go s
go (S.Effect ms) = do
print "effect"
result <- ms
go result
go (S.Yield a s) = do
putStrLn $ show "yield: " ++ show a
hFlush stdout
go sI had to modify your code a bit because it wasn't typechecking against the latest version of |
|
That seems to suggest that the JSON could not be parsed. You'll only get the Could it be that the |
|
I'm interested to see this merged, seems to me the only conflict is in context (base upper bound has been raised in servant-client-core.cabal) |
|
@voidus Ping me in the PR you open so that I can review & merge. :) |
|
It took me a bit to figure out the non-JSON version, so in case it helps anyone: import Network.HTTP.Client.TLS ( newTlsManager )
import Servant.API
( ServerSentEvents, EventKind(RawEvent) )
import Servant.Client.Core.ServerSentEvents (Event (Event), EventStreamT (unEventStreamT))
import Servant.Client.Streaming
( ClientM,
BaseUrl(BaseUrl),
Scheme(Http),
client,
mkClientEnv,
withClientM )
import Servant.Types.SourceT (StepT (..), SourceT (unSourceT))
import Data.Data (Proxy(..))
import GHC.IO.Handle (hFlush)
import GHC.IO.Handle.FD (stdout)
import qualified Data.ByteString.Char8 as BS
type MyAPI = ServerSentEvents 'RawEvent (Event BS.ByteString)
newsAPI :: ClientM (EventStreamT IO)
newsAPI = client (Proxy @MyAPI)
main :: IO ()
main = do
mgr <- newTlsManager
let env = mkClientEnv mgr $ BaseUrl Http "localhost" 8080 ""
withClientM newsAPI env $ \case
Left err -> (print err)
Right stream -> do
putStrLn "operating on stream obj"
-- let sourceT = unJsonEventStreamT stream
let sourceT = unEventStreamT stream
unSourceT sourceT go
where
go :: StepT IO (Event BS.ByteString) -> IO ()
go Stop = return ()
go (Error err) = do
putStrLn "error"
print err
go (Skip s) = do
putStrLn "skip"
go s
go (Effect ms) = do
putStrLn "effect"
result <- ms
go result
go (Yield (Event name dat) s) = do
putStrLn $ "yield: " ++ BS.unpack dat
hFlush stdout
go s |
|
Any ideas on how the SSE client and https://hackage.haskell.org/package/servant-event-stream should work together? |
This PR is an attempt to implement SSE for the Servant client-side.
Small example