# Maintain connection in ZMQ

**URL:** <https://discourse.julialang.org/t/maintain-connection-in-zmq/34243>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 6, 2020, 9:20am UTC](https://discourse.julialang.org/t/maintain-connection-in-zmq/34243 "2020-02-06T09:20:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [February 6, 2020, 9:20am UTC](https://discourse.julialang.org/t/maintain-connection-in-zmq/34243/1 "2020-02-06T09:20:25Z")

</div>

I am trying to establish a socket that listens for requests. Unfortunately, due to a combination of sparse documentation and my lack of familiarity with networking, I am unable to accomplish this through normal means.

This [part](https://github.com/JuliaInterop/ZMQ.jl/blob/master/src/sockopts.jl) of the package code seems to suggest that I can configure certain behaviors in ZQM. However, I am not sure how to do it. Here is my best attempt:

```julia
using ZMQ 
ctx = Context()
s1 = Socket(ctx, REP)
setproperty!(s1, :set_tcp_keepalive, 1)

```

which results in the following error:

```julia
ERROR: Socket has no field set_tcp_keepalive
Stacktrace:
 [1] setproperty!(::Socket, ::Symbol, ::Int64) at /home/dfish/.julia/packages/ZMQ/KdNw3/src/sockopts.jl:133
 [2] top-level scope at none:0

```

Would someone be willing to provide a MWE example showing how to continuously listen for requests?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [February 6, 2020, 2:41pm UTC](https://discourse.julialang.org/t/maintain-connection-in-zmq/34243/2 "2020-02-06T14:41:04Z")

</div>

I was able to figure out how to establish continuous communication between a client and server. I will post the code below in case it is useful for someone in the future.

Nonetheless, it would be useful to have some instruction for changing the configuration via `setproperty!`. This information is not available in the ZMQ documentation.

```julia
using ZMQ

function server_listen(server)
   #setproperty!(s1, :set_tcp_keepalive, 1)
    while true
       msg = recv(server, String)
       println("Server received: ", msg)
       send(server, "Client request processed.")
   end
end

function client_send(client)
   for i in 1:1000
      send(client, string("Client request # ", i))
      msgin = recv(client, String)
      println("Request status: ", msgin)
   end
end

# initialize connections
ctx = Context()
server = Socket(ctx, REP)
bind(server, "tcp://127.0.0.1:5555")
client = Socket(ctx, REQ)
connect(client, "tcp://127.0.0.1:5555")

# start server
@async server_listen(server)

# stress test
[client_send(client) for _ in 1:5]
close(server)
close(client)

```
