seanmor5
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:
- Receive data from another node
- 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 Responses
benwilson512
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
It is probably enough to use Process.send_after in the long-living process.








