Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Created September 20, 2025 21:28
Show Gist options
  • Save KonnorRogers/939186acf65ae5529497974ff5824760 to your computer and use it in GitHub Desktop.
Save KonnorRogers/939186acf65ae5529497974ff5824760 to your computer and use it in GitHub Desktop.
Trying to get DR SSE working
def tick args
args.state.clients ||= {}
# by default the embedded webserver is disabled in a production build
# to enable the http server in a production build you need to:
# - update metadata/cvars.txt
# - manually start the server up with enable_in_prod set to true:
args.state.port = 3000
GTK.start_server! port: args.state.port, enable_in_prod: true
# args.outputs.background_color = [0, 0, 0]
args.outputs.labels << { x: 640,
y: 360,
text: "Point your web browser at http://localhost:#{args.state.port}/ and open console.",
size_px: 30,
anchor_x: 0.5,
anchor_y: 0.5 }
args.outputs.labels << { x: 640,
y: 360,
text: "See metadata/cvars.txt for webserer configuration requirements.",
size_px: 30,
anchor_x: 0.5,
anchor_y: 1.5 }
if Kernel.tick_count == 1
GTK.openurl "http://localhost:3000"
end
args.inputs.http_requests.each do |req|
if req.uri == "/sse"
headers = {
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'Connection' =>'keep-alive'
}
if !args.state.clients[req.id]
# this has no way to get cleaned up and is a memory leak technically.
args.state.clients[req.id] = req
req.respond(
200,
"data: request_id: #{req.id}\n\n",
headers
)
else
# This never sends, because the request is blasted away and a new id is created.
req.respond(
200,
[
"event: update",
"data: #{Kernel.tick_count}\n\n",
].join(""),
headers
)
end
# How do we keep this "request" open and keep responding??
# Something like??
# req.on_close { args.state.clients.delete(req) }
# How do we send more requests? Does this `http_requests` stay keep the above request in its "inputs"?
else
js = <<~JS
const evtSource = new EventSource("/sse");
evtSource.onmessage = (e) => {
console.log("CONNECTED: ", e.data)
};
evtSource.addEventListener("update", (e) => {
console.log("UPDATE: ", e.data)
})
evtSource.onerror = (e) => {
console.error("event source disconnected.")
// console.log("Event Source State: ", evtSource.readyState)
}
JS
req.respond(
200,
[
"<!doctype html>",
"<html>",
"<head>",
"</head>",
"<body>",
"<script>",
js,
"</script>",
"</body>",
"</html>"
].join("")
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment