HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulewuss-2.0.1.9Haskell2010

Wuss

Wuss is a library that lets you easily create secure WebSocket clients over the WSS protocol. It is a small addition to the websockets package and is adapted from existing solutions by @jaspervdj, @mpickering, and @elfenlaid.

Example

import Wuss

import Control.Concurrent (forkIO)
import Control.Monad (forever, unless, void)
import Data.Text (Text, pack)
import Network.WebSockets (ClientApp, receiveData, sendClose, sendTextData)

main :: IO ()
main = runSecureClient "echo.websocket.org" 443 "/" ws

ws :: ClientApp ()
ws connection = do
    putStrLn "Connected!"

    void . forkIO . forever $ do
        message <- receiveData connection
        print (message :: Text)

    let loop = do
            line <- getLine
            unless (null line) $ do
                sendTextData connection (pack line)
                loop
    loop

    sendClose connection (pack "Bye!")

Retry

Note that it is possible for the connection itself or any message to fail and need to be retried. Fortunately this can be handled by something like the retry package. See this comment for an example.

  • 1 type
  • 4 values
  • Packagewuss-2.0.1.9
  • Exports5
  • LanguageHaskell2010
  • LicenceMIT
  • SourceWuss.hs
valuerunSecureClientWith
  1. :: (MonadIO m, MonadMask m)
  2. => HostName

    Host

  3. -> PortNumber

    Port

  4. -> String

    Path

  5. -> ConnectionOptions

    Options

  6. -> Headers

    Headers

  7. -> ClientApp a

    Application

  8. -> m a
#

A secure replacement for Network.WebSockets.runClientWith.

Example4 expressions
let options = defaultConnectionOptionslet headers = []let app _connection = return ()runSecureClientWith "echo.websocket.org" 443 "/" options headers app

If you want to run a secure client without certificate validation, use Network.WebSockets.runClientWithStream. For example:

let host = "echo.websocket.org"
let port = 443
let path = "/"
let options = defaultConnectionOptions
let headers = []
let tlsSettings = TLSSettingsSimple
    -- This is the important setting.
    { settingDisableCertificateValidation = True
    , settingDisableSession = False
    , settingUseServerName = False
    }
let connectionParams = ConnectionParams
    { connectionHostname = host
    , connectionPort = port
    , connectionUseSecure = Just tlsSettings
    , connectionUseSocks = Nothing
    }

context <- initConnectionContext
connection <- connectTo context connectionParams
stream <- makeStream
    (fmap Just (connectionGetChunk connection))
    (maybe (return ()) (connectionPut connection . toStrict))
runClientWithStream stream host path options headers $ \ connection -> do
    -- Do something with the connection.
    return ()