# HTTP.jl websockets - iterator?

**URL:** <https://discourse.julialang.org/t/http-jl-websockets-iterator/43299>\
**Category:** New to Julia\
**Created:** [July 18, 2020, 5:45pm UTC](https://discourse.julialang.org/t/http-jl-websockets-iterator/43299 "2020-07-18T17:45:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [July 18, 2020, 5:45pm UTC](https://discourse.julialang.org/t/http-jl-websockets-iterator/43299/1 "2020-07-18T17:45:30Z")

</div>

I’m not sure if this is an `HTTP.jl` usage question or really a design question, but in any case…

I’m trying to code an application for which I can seamlessly swap the bit that provides it with streaming data, and I want to be able to support both websockets and plain text files.

So right now I have for the websockets version, something like this:

```julia
HTTP.WebSockets.open(wsuri) do ws
    write(ws, ws_auth_msg)
    write(ws, subscribe_msg)
    while true
        # my app code
    end
end

```

for the file version I have

```julia
lines = open("streaming_data.txt") do f
    readlines(f)
end;

for line in lines
    # my app code
end

```

I would like the two situations be more similar. I would like something like

```julia
mutable struct App
    stream::DataStream
    # other things
end

function run(app::App)
    for line in app.stream
        # my app code
    end
end

app = App(stream)
run(app)

```

So, first question is: is this a reasonable design? Second question is: if so, how do I wrap an `HTTP.jl` websocket in a way that achieves this?

EDIT: I quite like the iterator syntax:

```julia
while ~isempty(it)
    x = popfirst!(it)
    # my app
end

```

To use with the file, we could do

```julia
lines = open("streaming_data.txt") do f
    readlines(f)
end;
it = Iterators.Stateful(lines)
while ~isempty(it)
    # my app
end

```

What about the `HTTP.jl` websocket?
