# Return from Websockets.open do block

**URL:** <https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879>\
**Category:** Web Stack\
**Tags:** question, package\
**Created:** [September 7, 2022, 10:59am UTC](https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879 "2022-09-07T10:59:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 7, 2022, 10:59am UTC](https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879/1 "2022-09-07T10:59:02Z")

</div>

I have a scenario in which I’m intentionally sending a wrong message to a websocket and it returns me the right input; I need to parse and store the right input and use it across the application. But I can’t return it. Here’s what the code looks like right now:

```julia
rightmsg = WebSockets.open(url) do ws 
                            wrongmsg = generatewrongmsg()
                            if writeguarded(ws, JSON3.write(wrongmsg)
                               data, success = readguarded(ws)
                               # parse data here 
                               return data
                           end 
                  end

```

But it just returns a `HTTP.Messages.Response` object. I can assign it to a global, but this block is not in a global scope; it’s inside a function and Julia doesn’t seem to have a way to capture a non-global, local variable. Any solution?

---

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 7, 2022, 11:17am UTC](https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879/2 "2022-09-07T11:17:08Z")

</div>

Ok I figured out a solution. Just need to write to a 1-sized `Channel` and then return with `take!(ch)`. Still feels a bit tacky.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 7, 2022, 12:42pm UTC](https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879/3 "2022-09-07T12:42:46Z")

</div>

See the docs for the `global` keyword arg too: [Essentials · The Julia Language](https://docs.julialang.org/en/v1.8/base/base/#global)

---

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 8, 2022, 5:35am UTC](https://discourse.julialang.org/t/return-from-websockets-open-do-block/86879/4 "2022-09-08T05:35:12Z")

</div>

Does this work in non-global contexts? What I have is:

```julia
function socketstuff()
       local rightmsg # It had to be local cuz naming conflicts in the global scope (for API reasons)
       WebSockets.open(url) do ws 
                            wrongmsg = generatewrongmsg()
                            if writeguarded(ws, JSON3.write(wrongmsg)
                               data, success = readguarded(ws)
                               # parse data here 
                               global rightmsg = data ### Am I even doing right???
                           end 
                  end

```
