seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

Async TCP Client/Server

Hey all, I’m implementing the Swirld Hashgraph Conensus Algorithm in Elixir right now. I have the basics of the Algorithm down; however, I want to start to test it.

Basically, I want to create a TCP server capable of discovering other nodes on the network (using mDNS, Erlang has this) that will both Listen and Send in parallel.

For those of you unfamiliar with the algorithm, basically a node running on the network performs 2 operations in parallel:

  1. Receive data from another node
  2. Simultaneously send data to a randomly selected on the network

I know how to implement a basic TCP server in Elixir using :gen_tcp and a GenServer, but is it possible to perform these two operations asynchronously on the same node? Is it as simple as using spawn or the Task API?

I don’t work with TCP and this kind of stuff very often, so I’m not exactly sure where to go with this.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yup. Processes are always async with respect to each other. If you’ve got an erlang node and you want multiple things to run at the same time, just spawn a process to do each thing. If that process will be short lived, use a Task. If it will be long lived, you probably want to spawn a GenServer, which is a regular process + some “best practice” conventions around life cycle management.

kokolegorille

kokolegorille

It is probably enough to use Process.send_after in the long-living process.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement