All Projects → Azolo → Websockex

Azolo / Websockex

Licence: mit
An Elixir Websocket Client

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Websockex

Websocket Client
🔧 .NET/C# websocket client library
Stars: ✭ 297 (-2.62%)
Mutual labels:  websocket, websocket-client
Python Slack Sdk
Slack Developer Kit for Python
Stars: ✭ 3,307 (+984.26%)
Mutual labels:  websocket, websocket-client
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (-54.75%)
Mutual labels:  websocket, websocket-client
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+336.39%)
Mutual labels:  websocket, websocket-client
Ws
Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
Stars: ✭ 17,419 (+5611.15%)
Mutual labels:  websocket, websocket-client
Php Wss
Web-socket server/client with multi-process and parse templates support on server and send/receive options on client
Stars: ✭ 117 (-61.64%)
Mutual labels:  websocket, websocket-client
Libhv
🔥 比libevent、libuv更易用的国产网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket client/server.
Stars: ✭ 3,355 (+1000%)
Mutual labels:  websocket, websocket-client
Recws
Reconnecting WebSocket is a websocket client based on gorilla/websocket that will automatically reconnect if the connection is dropped and keeps the connection alive - thread safe!
Stars: ✭ 60 (-80.33%)
Mutual labels:  websocket, websocket-client
Bolt Python
A framework to build Slack apps using Python
Stars: ✭ 190 (-37.7%)
Mutual labels:  websocket, websocket-client
Claws
Awesome WebSocket CLient - an interactive command line client for testing websocket servers
Stars: ✭ 187 (-38.69%)
Mutual labels:  websocket, websocket-client
Arduinowebsockets
arduinoWebSockets
Stars: ✭ 1,265 (+314.75%)
Mutual labels:  websocket, websocket-client
Node Slack Sdk
Slack Developer Kit for Node.js
Stars: ✭ 2,988 (+879.67%)
Mutual labels:  websocket, websocket-client
Sketchpad
Sketchpad is fully customisable collaborative whiteboard plugin written in pure JavaScript.
Stars: ✭ 85 (-72.13%)
Mutual labels:  websocket, websocket-client
Bolt Js
A framework to build Slack apps using JavaScript
Stars: ✭ 1,971 (+546.23%)
Mutual labels:  websocket, websocket-client
Deno Websocket
🦕 A simple WebSocket library like ws of node.js library for deno
Stars: ✭ 74 (-75.74%)
Mutual labels:  websocket, websocket-client
Embedded Jetty Websocket Examples
Embedded Jetty WebSocket Examples
Stars: ✭ 159 (-47.87%)
Mutual labels:  websocket, websocket-client
Jiny
Lightweight, modern, simple JVM web framework for rapid development in the API era
Stars: ✭ 40 (-86.89%)
Mutual labels:  websocket, websocket-client
Websocket
A PHP implementation of WebSocket.
Stars: ✭ 54 (-82.3%)
Mutual labels:  websocket, websocket-client
Flutter socket io
Socket IO supprt for flutter. Looking for contributors Swift and Java.
Stars: ✭ 170 (-44.26%)
Mutual labels:  websocket, websocket-client
Websocket Client
WebSocket client for Python
Stars: ✭ 2,810 (+821.31%)
Mutual labels:  websocket, websocket-client

WebSockex

Build Status Build status

An Elixir Websocket Client.

A simple implementation could be

defmodule WebSocketExample do
  use WebSockex

  def start_link(url, state) do
    WebSockex.start_link(url, __MODULE__, state)
  end

  def handle_frame({type, msg}, state) do
    IO.puts "Received Message - Type: #{inspect type} -- Message: #{inspect msg}"
    {:ok, state}
  end

  def handle_cast({:send, {type, msg} = frame}, state) do
    IO.puts "Sending #{type} frame with payload: #{msg}"
    {:reply, frame, state}
  end
end

See the examples/ directory for other examples or take a look at the documentation.

Installation

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

def deps do
  [{:websockex, "~> 0.4.2"}]
end

With Elixir releases prior to version 1.4

Ensure websockex is started before your application:

def application do
  [applications: [:websockex]]
end

Why WebSockex?

WebSockex was conceived after trying other libraries and realizing that I needed something tested, that actually implemented the spec, provided information about the connection, and could fit into a supervision tree. There was nothing that really fit into all those categories, so WebSockex was created.

There are other libraries out there can fit into some of the categories, but I consider WebSockex the best option if you want a callback inspired approach where most of the protocol workings are abstracted out of your way.

If you are afraid that WebSockex isn't stable enough or have some other problem with it that you don't feel like telling me about, then I would suggest the excellent gun library. It's a bit harder to use, and requires some knowledge of the spec. However it is an excellent library.

Supervision and Linking

A WebSockex based process is a easily able to fit into any supervision tree. It supports all the necessary capabilites to do so. In addition, it supports the Supervisor children format introduced in Elixir 1.5. So in any version of Elixir after 1.5, you can simply do:

defmodule MyApp.Client do
  use WebSockex

  def start_link(state) do
    WebSockex.start_link("ws://myapp.ninja", __MODULE__, state)
  end
end

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      {MyApp.Client, ["WebSockex is Great"]}
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

See the Supervision section in the WebSockex module documentation for more details on how this works.

WebSockex also supports both linked(start_link/3) and unlinked(start/3) processes.

iex> {:ok, pid} = WebSockex.start_link(url, __MODULE__, state)
iex> {:ok, pid} = WebSockex.start(url, __MODULE__, state)

However, the recommendation is to always use start_link/3 and if necessary trap exits.

This is because start/3 creates a detached process and has the capability to produce zombie processes outside of any application tree. This is generally a good piece of advice for any process, however since a module using WebSockex bevhaviour can be written as a self-sustaining tcp connection. I feel like it is even more important to express this particular piece of advice here.

Tips

Terminating with :normal after an Exceptional Close or Error

Usually you'll want to negotiate and handle any abnormal close event or error leading to it, as per WS Spec, but there might be cases where you simply want the socket to exit as if it was a normal event, even if it was abruptly closed or another exception was raised. In those cases you can define the terminate callback and return exit(:normal) from it.

def terminate(reason, state) do
    IO.puts(\nSocket Terminating:\n#{inspect reason}\n\n#{inspect state}\n")
    exit(:normal)
end

Debugging

WebSockex supports the debugging mechanism for OTP Special Processes provided through the :sys module.

Since WebSockex rolls its own Special Process implementation, it's able to provide a lot more information than a regular GenServer.

If, for example, I enable tracing with EchoClient from the examples (with Logger off), I would get this:

iex> {:ok, pid} = EchoClient.start_link(debug: [:trace])
*DBG* #PID<0.371.0> attempting to connect
*DBG* #PID<0.371.0> sucessfully connected
{:ok, #PID<0.371.0>}
iex> EchoClient.echo(pid, "Hello")
*DBG* #PID<0.371.0> sending frame: {:text, "Hello"}
:ok
*DBG* #PID<0.371.0> received frame: {:text, "Hello"}
*DBG* #PID<0.371.0> received frame: :ping
*DBG* #PID<0.371.0> replying from :handle_ping with :pong
iex> EchoClient.echo(pid, "Close the things!")
*DBG* #PID<0.371.0> sending frame: {:text, "Close the things!"}
:ok
*DBG* #PID<0.371.0> received frame: {:text, "Close the things!"}
*DBG* #PID<0.371.0> closing with local reason: {:local, :normal}
*DBG* #PID<0.371.0> sending close frame: {:local, :normal}
*DBG* #PID<0.371.0> forcefully closed the connection because the server was taking too long close

You could also enable tracing after a process has started like this:

iex> {:ok, pid} = EchoClient.start_link()
iex> :sys.trace(pid, true)
:ok
iex> EchoClient.echo(pid, "Hi")
*DBG* #PID<0.379.0> sending frame: {:text, "Hi"}
:ok
*DBG* #PID<0.379.0> received frame: {:text, "Hi"}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].