Last active
September 24, 2022 08:51
-
-
Save jhrcek/f223dfda43e5f65e6a4f7a7ef02f22c0 to your computer and use it in GitHub Desktop.
Server-Sent Events - is it possible to detect SSE client disconnecting?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- stack script | |
--resolver lts-18.28 | |
--package bytestring,containers,servant,servant-server,servant-rawm-server,stm,wai,wai-extra,warp | |
-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE ImportQualifiedPost #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeOperators #-} | |
module Main where | |
import Control.Concurrent.Chan (Chan, newChan, writeChan) | |
import Control.Concurrent.STM.TVar (TVar, newTVarIO, readTVar, readTVarIO, writeTVar) | |
import Control.Monad.IO.Class (liftIO) | |
import Control.Monad.STM (atomically) | |
import Data.ByteString.Builder (string8) | |
import Data.Data (Proxy (..)) | |
import Data.Map.Strict (Map) | |
import Data.Map.Strict qualified as Map | |
import Network.Wai.EventSource (ServerEvent (..), eventSourceAppChan) | |
import Network.Wai.Handler.Warp (run) | |
import Servant (Handler (..)) | |
import Servant.API (Capture, NoContent (..), PostNoContent, type (:<|>) (..), type (:>)) | |
import Servant.RawM.Server (RawM) | |
import Servant.Server (Application, serve) | |
type Api = SseRoute :<|> HelloRoute | |
-- SSE Endpoint where User with given ID can listen to messages sent to them | |
type SseRoute = "listen" :> Capture "UserId" UserId :> RawM | |
-- Normal HTTP endpoint where users can send messages to other users | |
type HelloRoute = "hello" :> Capture "From" UserId :> Capture "To" UserId :> PostNoContent | |
type UserId = Int | |
-- Each listening user will have their dedicated Chan, through which we send messages addressed to them | |
type ChanMap = Map UserId (Chan ServerEvent) | |
main :: IO () | |
main = do | |
putStrLn $ | |
unlines | |
[ "Listen to messages as user with id A:" | |
, " curl http://localhost:8000/listen/A" | |
, "As user with id A send message to user with id B" | |
, " curl -XPOST http://localhost:8000/hello/A/B" | |
] | |
chanMap <- newTVarIO Map.empty | |
run 8000 (app chanMap) | |
app :: TVar ChanMap -> Application | |
app chanMapVar = | |
serve (Proxy @Api) (listenH :<|> helloH) | |
where | |
listenH :: UserId -> Handler Application | |
listenH userId = Handler $ do | |
chan <- liftIO newChan | |
userChan <- liftIO . atomically $ do | |
chanMap <- readTVar chanMapVar | |
case Map.lookup userId chanMap of | |
-- THE PROBEM: as more users listen, we only ever insert new Chans in this Map | |
-- Is there a way to detect that given listener disconnected (so I can remove their Chan from the map) | |
Nothing -> writeTVar chanMapVar (Map.insert userId chan chanMap) >> pure chan | |
Just userChan -> pure userChan | |
pure (eventSourceAppChan userChan) | |
helloH :: UserId -> UserId -> Handler NoContent | |
helloH senderId recipientId = Handler $ do | |
chanMap <- liftIO $ readTVarIO chanMapVar | |
case Map.lookup recipientId chanMap of | |
Nothing -> pure () -- Recipient is not connected, not sending anything | |
Just recipientChan -> | |
liftIO . writeChan recipientChan $ | |
ServerEvent Nothing Nothing [string8 $ "User " <> show senderId <> " says hi"] | |
pure NoContent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment