Skip to content

Instantly share code, notes, and snippets.

@palkan
Created October 8, 2022 18:15
Show Gist options
  • Save palkan/e59e195bea88eebb0b2b9daa899589cb to your computer and use it in GitHub Desktop.
Save palkan/e59e195bea88eebb0b2b9daa899589cb to your computer and use it in GitHub Desktop.
Action Cable image publisher
<div class="min-w-full flex flex-row">
<%= turbo_stream_from "demo" %>
<div id="demo"></div>
</div>
#!/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