Replies: 3 comments 5 replies
-
Unpoly will not open a WebSocket connection for you. However, if you set up that connection yourself, you can pass HTML fragments to Unpoly to render. See rendering strings of HTML in the docs. |
Beta Was this translation helpful? Give feedback.
2 replies
-
just fyi: |
Beta Was this translation helpful? Give feedback.
2 replies
-
You don't necessarily need to use WebSockets either. It will work fine with server-side events as well. require "sinatra"
require "async"
get "/" do
<<~HTML
<!DOCTYPE html>
<html>
<head>
<script type="importmap">
{
"imports": {
"unpoly": "https://esm.sh/[email protected]/unpoly.js"
}
}
</script>
<script type="module">
import * as unpoly from "unpoly"
const source = new EventSource('stream')
source.onerror = () => source.close()
source.onmessage = (e) => {
up.render({fragment: e.data })
}
</script>
</head>
<body>
<div id="foo">This is the original foo element</div>
</body>
</html>
HTML
end
get "/stream", provides: "text/event-stream" do
stream do |out|
Sync do |task|
task.async do
sleep 10
stream_html(out, <<~HTML)
<div id="foo">This is the last foo</div>
HTML
end
task.async do
sleep 2
stream_html(out, <<~HTML)
<div id="foo">This is a new foo</div>
HTML
end
end
end
end
def stream_html(out, fragment)
out << "data: #{fragment.gsub("\n", "")}\n\n"
end |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Apologies for bringing up a non-issue. I want to use unpoly on my website and exchange HTML fragments with my backend through WebSockets. Does unpoly currently support doing that?
Beta Was this translation helpful? Give feedback.
All reactions