Chapter 19. Wiki: Markdown, Chat Subsite, Event Source
This example will tie together a few different ideas. We’ll start with a chat subsite, which allows us to embed a chat widget on any page. We’ll use the HTML5 event source API to handle sending events from the server to the client.
-- @Chat.hs
{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes,
TemplateHaskell, FlexibleInstances, MultiParamTypeClasses,
FlexibleContexts
#-}
-- | This
module
defines
a
subsite
that
allows
you
to
insert
a
chat
box
on
-- any page of your site. It uses eventsource for sending the messages from
-- the server to the browser.
module
Chat
where
import
Yesod
import
Control.Concurrent.Chan
(
Chan
,
dupChan
,
writeChan
)
import
Data.Text
(
Text
)
import
Network.Wai.EventSource
(
ServerEvent
(
..
),
eventSourceAppChan
)
import
Language.Haskell.TH.Syntax
(
Type
(
VarT
),
Pred
(
ClassP
),
mkName
)
import
Blaze.ByteString.Builder.Char.Utf8
(
fromText
)
import
Data.Monoid
(
mappend
)
-- | Our subsite foundation. We keep a channel of events that all connections
-- will share.
data
Chat
=
Chat
(
Chan
ServerEvent
)
-- | We need to know how to check if a user is logged in and how to get
-- his/her username (for printing messages).
class
(
Yesod
master
,
RenderMessage
master
FormMessage
)
=>
YesodChat
master
where
getUserName
::
GHandler
sub
master
Text
isLoggedIn
::
GHandler
sub
master
Bool
-- Now we set up our subsite. The first argument is the subsite, very similar
-- to how we've used mkYesod in the past. The second argument ...
Get Developing Web Applications with Haskell and Yesod now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.