exchangeloha.blogg.se

Elixir http client
Elixir http client






  1. #ELIXIR HTTP CLIENT HOW TO#
  2. #ELIXIR HTTP CLIENT DRIVER#

We need a GenServer so that we will be able to keep the TCP socket in the GenServer’s state and reuse that socket for all commication. We’ll use a GenServer as the only interface with the TCP connection. That’s all we need to know about TCP connections for now, let’s move on. By default, the process that calls connect/3 is the “controlling process” of the TCP connection, which means that TCP messages from the socket will be delivered to it. To establish a connection with a TCP server we use :gen_tcp.connect/3 passing the host (as a charlist, damn you Erlang!), the port and a list of options. As we will see later on, we can control how messages are delivered to the client process with the value of the :active option on the TCP socket.

elixir http client

Messages sent from the server to the client are usually delivered to the client process as Erlang messages, so it’s straightforward to work with them. In this article we’ll only set up clients that connect to an external TCP server, but the :gen_tcp module can also be used to set up TCP servers.Īll messages to the server are sent using :gen_tcp.send/2. In Erlang and Elixir, TCP connections are handled using the :gen_tcp module. Let’s get started! Brief overview of TCP connections in Erlang/Elixir Since there’s no point in coming up with a clever name for the library we’re going to write, we’ll just call it Redis. Redis uses its own protocol (more on this in a while) on top of TCP to exchange data, without relying on common protocols such as HTTP, but we will not focus on that: we will only deal with the TCP connection from Elixir to the Redis server.Ī little side note: obviously, there are several Erlang and Elixir libraries for talking to Redis, but bear with me. A Redis server is just a TCP server sends and receives messages.

#ELIXIR HTTP CLIENT DRIVER#

A semi-realistic exampleįor the sake of this article, we will build an almost working driver for the Redis key-value store. The principles we describe, however, are very similar for any other type of connection (for example, connections that use the UDP protocol). In this article we will only talk about TCP connections since TCP is probably the most common protocol used in network applications. This turns out to be useful if there are no external libraries for a particular service but also if we want to understand how these libraries work.

elixir http client

#ELIXIR HTTP CLIENT HOW TO#

Many times, the connection with the network service will be transparent to the programmer thanks to external libraries (e.g., database drivers), but I think it’s interesting to know how to handle such connections by hand. In this context, there’s often the need to connect to external services through the network: for example, a classic web application could connect to a relational database and a key-value store, while an application that runs on embedded systems could connect to other nodes on the network. Elixir is frequently used in network-aware applications because of the core design of Erlang and the Erlang VM.








Elixir http client