Giter VIP home page Giter VIP logo

mint_websocket_client's Introduction

MintWebsocketClient

CI

MintWebsocketClient is a behaviour on top of GenServer providing convenient interface to build websocket clients.

Using Mint and Mint.WebSocket under the hood.

Usage

Add mint_websocket_client to your list of dependencies in mix.exs:

def deps do
  [
    {:mint_websocket_client, "~> 0.1.1"}
  ]
end

Next, run mix deps.get in your shell to fetch and compile MintWebsocketClient. Start an interactive Elixir shell with iex -S mix:

Examples

Keep in mind frame you return in {:reply, frame, state} might be lost depends on the connection state.

defmodule WS do
  use MintWebsocketClient

  def start_link(url, opts \\ []) do
    opts =
      opts
      |> Keyword.put_new(:protocols, [:http1])
      |> Keyword.put_new(:transport_opts, [verify: :verify_none])

    MintWebsocketClient.start_link(url, __MODULE__, opts)
  end

  @impl true
  def handle_connect(status_map, state) do
    IO.inspect(status_map, label: "handle_connect")
    {:ok, state}
  end

  @impl true
  def handle_disconnect(reason, state) do
    IO.inspect(reason, label: "handle_disconnect")
    {:reconnect, state}
  end

  @impl true
  def handle_frame(frame, state) do
    IO.inspect(frame, label: "handle_frame")
    {:ok, state}
  end

  @impl true
  def terminate(reason, _state) do
    IO.inspect(reason, label: "terminate")
  end
end

iex(1)> {:ok, pid} = WS.start_link("wss://ws.postman-echo.com/raw")
{:ok, #PID<0.207.0>}
handle_connect: %{
  headers: [...],
  status: 101
}

iex(2)> MintWebsocketClient.send_frame(pid, :ping)
:ok
handle_frame: {:pong, ""}

iex(3)> MintWebsocketClient.send_frame(pid, :close)
:ok
handle_disconnect: %Mint.TransportError{reason: :closed}

iex(4)> MintWebsocketClient.send_frame(pid, {:text, "{\"lang\":\"elixir\"}"})
:ok
handle_frame: {:text, "{\"lang\":\"elixir\"}"}

mint_websocket_client's People

Contributors

vshev4enko avatar dependabot[bot] avatar

Stargazers

Hissssst avatar

Watchers

Lucian avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.