ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - meh/reagent: You need more reagents to conjure this server.
You need more reagents to conjure this server. Contribute to meh/reagent development by creating an account on GitHub.
Visit Site

GitHub - meh/reagent: You need more reagents to conjure this server.

GitHub - meh/reagent: You need more reagents to conjure this server.

reagent - socket acceptor pool

reagent is a socket acceptor pool for Elixir that leverages the socket library and its protocols to provide an easy way to implement servers.

Getting started

To define a reagent you first have to define a module using the reagent behaviour. This will define some basic functions you can extend and other helpers on the module and will make it startable as a reagent.

defmodule Test do
  use Reagent.Behaviour
end

When you want to start a server running the defined reagent, you have to call Reagent.start. It takes as first parameter the module implementing the behaviour and as second parameter a listener descriptor.

Listener descriptors contain the definition of the listener, including port, whether they're secure or not, other socket options and starting environment.

Reagent behaviour

A reagent to do anything useful has to either implement handle/1 or start/1.

handle/1 is called by the default start/1 and it gets called as a replacement for the acceptor process. It gets called with a Reagent.Connection record.

This is usually useful to implement simple protocols when you don't need a full blown gen_server or similar to handle a connection.

If you want more complex connection handling you can define start/1, it gets called with a Reagent.Connection record as well and must return { :ok, pid } or { :error, reason }. The returned process will be made owner of the socket and be used as reference for the connection itself.

You can also define accept/1 which gets called with the Reagent.Listener and allows you more fine grained socket acception.

Simple example

defmodule Echo do
  use Reagent

  def handle(conn) do
    case conn |> Socket.Stream.recv! do
      nil ->
        :closed

      data ->
        conn |> Socket.Stream.send! data

        handle(conn)
    end
  end
end

This is a simple implementation of an echo server.

To start it on port 8080 just run Reagent.start Echo, port: 8080.

Complex example

defmodule Echo do
  use Reagent

  def start(connection) do
    GenServer.start __MODULE__, connection, []
  end

  use GenServer

  def init(connection) do
    { :ok, connection }
  end

  # this message is sent when the socket has been completely accepted and the
  # process has been made owner of the socket, you don't need to wait for it
  # when implementing handle because it's internally handled
  def handle_info({ Reagent, :ack }, connection) do
    connection |> Socket.active!

    { :noreply, connection }
  end

  def handle_info({ :tcp, _, data }, connection) do
    connection |> Socket.Stream.send! data

    { :noreply, connection }
  end

  def handle_info({ :tcp_closed, _ }, connection) do
    { :stop, :normal, connection }
  end
end

This is the implementation of a full-blown gen_server based echo server (which is obviously overkill).

As with the simple example you just start it with Reagent.start Echo, port: 8080.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory