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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement