Skip to content

Instantly share code, notes, and snippets.

@nicopace
Created April 25, 2019 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicopace/149d64ff8dbf9441b6eb4e06bdc46f0d to your computer and use it in GitHub Desktop.
Save nicopace/149d64ff8dbf9441b6eb4e06bdc46f0d to your computer and use it in GitHub Desktop.
Server-sent events for openwrt on pure lua
<html>
<!-- this one goes in /www/ubuslisten.html -->
<meta charset="UTF-8">
<body></body>
<script>
var source = new EventSource("/cgi-bin/ubuslisten.lua");
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
</script>
</html>
#!/usr/bin/lua
-- this one goes in /www/cgi-bin/ubuslisten.lua
io.stdout:write("Content-Type: text/event-stream\n\n")
io.stdout:flush()
ubuslisten = io.popen("ubus listen")
for line in ubuslisten:lines() do
io.stdout:flush()
io.stdout:write("data: ")
io.stdout:write(line)
io.stdout:write("\n\n")
io.stdout:flush()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment