Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Created January 19, 2021 22:01
Show Gist options
  • Save peteristhegreat/6e7ba3c336ba21ff9cac3638bbcf93cd to your computer and use it in GitHub Desktop.
Save peteristhegreat/6e7ba3c336ba21ff9cac3638bbcf93cd to your computer and use it in GitHub Desktop.
Test SSE Route from Julia, subscribe to SSE route until timeout
using HTTP
# Developed against https://github.com/sitepoint-editors/server-sent-events-demo
# url="http://localhost:8080/sse"
function test_sse_channel(url="http://localhost:8080/sse", headers=[]; timeout=5)
# Read from the stream until the timeout occurs
# Note that timeout begins after the connection is started
# Based on the audio streaming example here
# https://github.com/JuliaWeb/HTTP.jl/blob/0464e7a3614e88ca09b17fa616fbd1b06237534a/src/HTTP.jl#L298
try
HTTP.open("GET", url, headers; retries=0, retry=false) do io
r = startread(io)
@show r.status
t = Timer(timeout)
while !eof(io) && isopen(t)
bytes = readavailable(io)
@show String(bytes)
end
close(io.stream)
return r
end
catch e
if !(isa(e, HTTP.IOExtras.IOError) && e.e == EOFError())
# Note that later versions of HTTP.jl probably don't need the filter in the if statement above
# https://github.com/JuliaWeb/HTTP.jl/blob/master/src/StreamRequest.jl#L89
showerror(e, catch_backtrace())
println(stderr, "")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment