packages feed

streamly-websockets-tls-0.1.0.0: README.md

# streamly-websockets-tls

Secure WebSockets, and WebSockets by URL, with
[Streamly](https://streamly.composewell.com/). This is the convenient face
of
[streamly-websockets](https://hackage.haskell.org/package/streamly-websockets):
the scheme of a URL (`ws` or `wss`) chooses between plain TCP and TLS
(through
[streamly-network-tls](https://hackage.haskell.org/package/streamly-network-tls)),
and a server can serve over TLS.

A client:

```haskell
{-# LANGUAGE OverloadedStrings #-}

import Streamly.External.Network.WebSocket (Message (TextMessage), receive,
                                            send)
import Streamly.External.Network.WebSocket.TLS (withUrl)

main :: IO ()
main = withUrl "wss://example.org/chat" $ \connection -> do
  send connection (TextMessage "hello")
  receive connection >>= print
```

A server over TLS that sends back what it receives:

```haskell
{-# LANGUAGE OverloadedStrings #-}

import Streamly.Data.Fold (drainMapM)
import Streamly.Data.Stream (fold)
import Streamly.External.Network.WebSocket.Server (defaultServerOptions,
                                                   messages, send)
import Streamly.External.Network.WebSocket.TLS (loadCredentials, serveSecure)

main :: IO ()
main = do
  credentials <- loadCredentials "certificate.pem" "key.pem"
  serveSecure credentials "0.0.0.0" 8443 defaultServerOptions $ \_ connection ->
    fold (drainMapM (send connection)) (messages connection)
```

## What it gives

- `withUrl`: the WebSocket of a `ws` or `wss` URL, checked against the
  certificates of the system.
- `urlConfig` and `withConnectionAt`: the same in two steps, to change the
  configuration (extra headers, bounds) or the certificates trusted.
- `serveSecure` and `serveSecureSocket`: a server over TLS, with the
  certificate and the key that `loadCredentials` reads.

## Building

To build against copies of the libraries this package is built on
([streamly-websockets](https://hackage.haskell.org/package/streamly-websockets),
[streamly-network-tls](https://hackage.haskell.org/package/streamly-network-tls),
[streamly-http](https://hackage.haskell.org/package/streamly-http))
checked out next to this repository, pass
`--project-file=cabal.project.siblings`.