# Async TCP Client/Server

**URL:** <https://forum.elixirforum.com/t/async-tcp-client-server/25202>\
**Category:** Questions\
**Tags:** mdns, tcp\
**Created:** [September 6, 2019, 1:55am UTC](https://forum.elixirforum.com/t/async-tcp-client-server/25202 "2019-09-06T01:55:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanmor5](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seanmor5/32/20610_2.png) [@seanmor5](https://forum.elixirforum.com/u/seanmor5)\
**Post date:** [September 6, 2019, 1:55am UTC](https://forum.elixirforum.com/t/async-tcp-client-server/25202/1 "2019-09-06T01:55:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [September 6, 2019, 2:44am UTC](https://forum.elixirforum.com/t/async-tcp-client-server/25202/2 "2019-09-06T02:44:07Z")

</div>

> [@seanmor5](#):
>
> Is it as simple as using `spawn` or the Task API?

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.

---

<div class="post-metadata">

**Author:** ![seanmor5](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seanmor5/32/20610_2.png) [@seanmor5](https://forum.elixirforum.com/u/seanmor5)\
**Post date:** [September 6, 2019, 2:51am UTC](https://forum.elixirforum.com/t/async-tcp-client-server/25202/3 "2019-09-06T02:51:39Z")

</div>

Thanks!

In the case of Sending data, if I want to repeatedly send some data to a random node on the network after some set time delay, is it best to do this in the same, long-living process? OR should a process be spawned every time I want to send something?

That is:

- spawn process, send data, delay, send data, delay  
OR
- spawn process, send data, kill process, delay, spawn process, send data, kill process, delay

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [September 6, 2019, 3:02am UTC](https://forum.elixirforum.com/t/async-tcp-client-server/25202/4 "2019-09-06T03:02:18Z")

</div>

It is probably enough to use Process.send\_after in the long-living process.
