Packages
glisten
0.1.3
9.0.1
9.0.0
9.0.0-rc1
8.0.3
8.0.2
8.0.1
8.0.0
8.0.0-rc1
7.0.1
7.0.0
6.0.0
5.0.0
4.0.0
3.0.0
2.0.0
1.0.0
0.11.0
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.0
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
a shiny Gleam TCP/TLS server
Current section
Files
Jump to
Current section
Files
README.md
# glisten
This is a gleam wrapper around `gen_tcp` loosely based on [ThousandIsland](https://github.com/mtrudel/thousand_island).
It uses the `gleam_otp` library to handle the supervisor and child processes.
The general structure is similar to ThousandIsland. There is a supervisor that
manages a pool of acceptors. Each acceptor will block on `accept` until a
connection is opened. The acceptor will then spawn a handler process and
then block again on `accept`.
The handler function loops on messages received from the socket. You can define
a handler with a function of the following type:
```gleam
fn(HandlerMessage, Socket) -> actor.Next(Socket)
```
This gives you access to the socket if you want to `send` to it in response. I
think right now I don't have this set up where you can send to the socket
unprovoked? So that seems like something I'll need to change... imminently.
## Examples
You can kind of do whatever you want.
I didn't test this, to be honest. I think this should work?
```gleam
try listener =
tcp.listen(
8000,
[
tcp.Active(
False
|> dynamic.from
|> dynamic.unsafe_coerce,
),
],
)
try socket = tcp.accept(listener)
try msg = tcp.do_receive(socket, 0)
io.println("got a msg")
io.debug(msg)
```
See [dew](https://github.com/rawhat/dew) for some better examples.