# Fresh

Fresh is an attempt to create a simple, reliable and flexible WebSocket client based on [Mint.WebSocket]( 🌱

> I recommend using an alternative WebSocket client such as [WebSockex]( until we have finished adding tests to identify and resolve potential common issues.

## Why Fresh?

Discover the reasons behind choosing Fresh over existing libraries, summed up in three key aspects:

- Simplicity
- Resilience
- Control

### Simplicity

Fresh is designed with simplicity in mind, offering a user-friendly API that includes clear examples and comprehensive documentation.

### Resilience

Fresh excels in ensuring a robust and enduring connection to the server. By default, Fresh promptly re-establishing connection when the server terminates the connection or encounters any connectivity issues. When used alongside Supervisor, Fresh delivers exceptional reliability.

### Control

With Fresh, you gain extensive control over the flow of WebSocket connections, including control over resilience. You can manage scenarios requiring reconnection, identify those that should be ignored, specify when to gracefully terminate a connection, and even instruct Supervisor to restart your process as needed. Additionally, Fresh allows you to leverage custom `Mint.WebSocket` options, transport layer options, custom headers, and more, providing you with the flexibility you require.

## Installation

Package can be installed by adding `fresh` to your list of dependencies in `mix.exs`:

defp deps do
    {:fresh, "~> 0.1.0-rc"}

## Example

Below is an example of a WebSocket client that handles incoming frames to implement a simple counter:

_Need to mention this example module contains approximately 15 lines of code._

defmodule EchoWebSocket do
  use Fresh

  def handle_connect(_status, _headers, _state) do
    IO.puts("Start counting from 0")
    {:reply, [{:text, "1"}], 0}

  def handle_in({:text, number}, _state) do
    number = String.to_integer(number)

    IO.puts("Number: #{number}")
    {:reply, [{:text, "#{number + 1}"}], number}

More examples can be found inside [examples/]( folder.

## Documentation

Check out [HexDocs website]( for documentation and API reference.

## License

Fresh is licensed under the MIT license.