# ZMQ Basics Publisher Example Crashes

**URL:** <https://discourse.julialang.org/t/zmq-basics-publisher-example-crashes/97700>\
**Category:** General Usage\
**Tags:** zmq\
**Created:** [April 20, 2023, 11:20am UTC](https://discourse.julialang.org/t/zmq-basics-publisher-example-crashes/97700 "2023-04-20T11:20:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![opiateblush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opiateblush/32/46925_2.png) [@opiateblush](https://discourse.julialang.org/u/opiateblush)\
**Post date:** [April 20, 2023, 11:20am UTC](https://discourse.julialang.org/t/zmq-basics-publisher-example-crashes/97700/1 "2023-04-20T11:20:13Z")

</div>

Hey, unfortunately there was no response to my [issue](https://github.com/JuliaInterop/ZMQ.jl/issues/223) on GitHub, so maybe someone over here got an idea what the problem might be:

The [example publisher](https://zguide.zeromq.org/docs/chapter1/#Getting-the-Message-Out) in the Basics section of the ZMQ Guide crashes if put in a function. See this:

```julia
using ZMQ

function main()
    context = Context()
    socket = Socket(context, PUB)
    bind(socket, "tcp://*:5556")
    
    while true
        zipcode = rand(10000:99999)
        temperature = rand(-80:135)
        relhumidity = rand(10:60)
        message = "$zipcode $temperature $relhumidity"
        send(socket, message)
        yield()
    end
    
    close(socket)
    close(context) 
end

if abspath(PROGRAM_FILE) == @ __FILE__
    main()
end

```

Which results in:

```julia
ERROR: LoadError: StateError("Unknown error")
Stacktrace:
 [1] _send(socket::Socket, zmsg::Base.RefValue{ZMQ._Message}, more::Bool)
   @ ZMQ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:14
 [2] send(socket::Socket, data::String; more::Bool)
   @ ZMQ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:45
 [3] send
   @ C:\Users\toby\.julia\packages\ZMQ\lrABE\src\comm.jl:42 [inlined]
 [4] main()
   @ Main C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:13
 [5] top-level scope
   @ C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:22
in expression starting at C:\Users\toby\Workspaces\ZMQTest.jl\publisher_basics.jl:21

```

Everthing is fine if the code is not encapsulated inside a function:

```julia
using ZMQ

context = Context()
socket = Socket(context, PUB)
bind(socket, "tcp://*:5556")

while true
    zipcode = rand(10000:99999)
    temperature = rand(-80:135)
    relhumidity = rand(10:60)
    message = "$zipcode $temperature $relhumidity"
    send(socket, message)
    yield()
end

close(socket)
close(context)

```

I’m on Windows 11 running Julia v1.8.5 with ZMQ v1.2.2.

Any idea what I could have done wrong? It seems to me like a rather simple change that couldn’t really hurt, but apparently I have been proven wrong and could have misused the API.
