The websockets package

[Tags:benchmark, bsd3, library, test]

A library for creating WebSocket-capable servers, where the implemented protocol is defined here:

This library is only tested with Chromium >=7 and Firefox >=4 and is a work in progress.

How do you use this library? Here's how:

And here's a short example of a server that accepts clients, greets them with a welcome message, checks for disconnects and replies to all messages by echoing them back with an appended meow:

 import Network.WebSockets (shakeHands, getFrame, putFrame)
 import Network (listenOn, PortID(PortNumber), accept, withSocketsDo)
 import System.IO (Handle, hClose)
 import qualified Data.ByteString as B (append, null)
 import Data.ByteString.UTF8 (fromString) -- this is from utf8-string
 import Control.Monad (forever)
 import Control.Concurrent (forkIO)

 main :: IO ()
 main = withSocketsDo $ do
   socket <- listenOn (PortNumber 12345)
   putStrLn "Listening on port 12345."
   forever $ do
     (h, _, _) <- accept socket
     forkIO (talkTo h)

 talkTo :: Handle -> IO ()
 talkTo h = do
   request <- shakeHands h
   case request of
     Left error -> putStrLn error
     Right rqst -> do
       putFrame h (fromString "滴水之恩当以涌泉相报")
       putStrLn "Shook hands, sent welcome message."
       talkLoop h

   hClose h
   putStrLn "Client quit, closed handle."

 talkLoop :: Handle -> IO ()
 talkLoop h = do
   msg <- getFrame h
   if B.null msg
      then return ()
      else do
        putFrame h $ B.append msg (fromString ", MEOW!")
        talkLoop h

The example above will suffice if you wish to accept any WebSocket-capable client, regardless of its origin or target. It won't suffice if you have to filter the incoming clients by the contents of their requests. For that, you can use getRequest and putResponse, which allow you to inspect the request details before you send back a response, if any.

If you have any suggestions, bug reports and/or fixes, feel free to send them to


Versions,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, (info)
Dependencies base (==4.*), binary (>=0.5), bytestring (>=0.9), containers (>=0.3), network (>=, pureMD5 (>=2.1) [details]
License BSD3
Copyright (c) 2010 Siniša Biđin
Author Siniša Biđin
Maintainer Siniša Biđin <>
Category Network
Bug tracker
Uploaded Wed Nov 10 20:58:08 UTC 2010 by SinisaBidin
Distributions Arch:, Debian:, LTSHaskell:, NixOS:, Stackage:, Tumbleweed:
Downloads 32286 total (2382 in the last 30 days)
0 []
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees