Created
October 8, 2022 18:15
-
-
Save palkan/e59e195bea88eebb0b2b9daa899589cb to your computer and use it in GitHub Desktop.
Action Cable image publisher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="min-w-full flex flex-row"> | |
<%= turbo_stream_from "demo" %> | |
<div id="demo"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Run this script as: | |
# | |
# bundle exec bin/publisher path/to/some.png | |
require_relative "../config/application" | |
Rails.application.initialize! | |
TEMPLATE = <<~HTML | |
<turbo-stream target="demo" action="replace"> | |
<template> | |
<div id="demo"> | |
<h2>Frame: %{number}</h2> | |
<img src="data:image/png;base64, %{data}" /> | |
</div> | |
</template> | |
</turbo-stream> | |
HTML | |
image = ARGF.read | |
data = Base64.strict_encode64(image) | |
stream_name = Turbo::StreamsChannel.send(:stream_name_from, "demo") | |
100.times do |number| | |
content = TEMPLATE % {number:, data:} | |
ActionCable.server.broadcast stream_name, content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment