Created
January 17, 2024 00:45
-
-
Save luizpvas/eb06be3334c4ef8232ed5f9d156062e0 to your computer and use it in GitHub Desktop.
Anycable reliable stream (demo)
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><%= item %></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
<div>This is page A</div> | |
<%= link_to "Page B", anycable_push_b_path, class: "link" %> | |
<%= turbo_stream_from "items-a" %> | |
<div id="items-a" class="m-4 p-4 border"> | |
<%= render partial: "anycable_demo/item", collection: items %> | |
</div> | |
<%= button_to "Push to page A", anycable_push_a_path, method: :post %> | |
<%= button_to "Push to page B", anycable_push_b_path, method: :post %> |
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
class AnycableDemoController < ApplicationController | |
def a | |
render "anycable_demo/a", locals: {items: list_a.elements} | |
end | |
def push_a | |
item = ::Time.current.to_i | |
list_a << item | |
::Turbo::StreamsChannel.broadcast_append_to( | |
"items-a", | |
target: "items-a", | |
partial: "anycable_demo/item", | |
locals: {item:} | |
) | |
end | |
def b | |
render "anycable_demo/b", locals: {items: list_b.elements} | |
end | |
def push_b | |
item = ::Time.current.to_i | |
list_b << item | |
::Turbo::StreamsChannel.broadcast_append_to( | |
"items-b", | |
target: "items-b", | |
partial: "anycable_demo/item", | |
locals: {item:} | |
) | |
end | |
private | |
def list_a | |
Kredis.list "list_a" | |
end | |
def list_b | |
Kredis.list "list_b" | |
end | |
end |
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>This is page B</div> | |
<%= link_to "Page A", anycable_push_a_path, class: "link" %> | |
<%= turbo_stream_from "items-b" %> | |
<div id="items-b" class="m-4 p-4 border"> | |
<%= render partial: "anycable_demo/item", collection: items %> | |
</div> | |
<%= button_to "Push to page A", anycable_push_a_path, method: :post %> | |
<%= button_to "Push to page B", anycable_push_b_path, method: :post %> |
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
scope "/anycable_demo" do | |
get "/a", to: "anycable_demo#a", as: :anycable_a | |
get "/b", to: "anycable_demo#b", as: :anycable_b | |
post "/a", to: "anycable_demo#push_a", as: :anycable_push_a | |
post "/b", to: "anycable_demo#push_b", as: :anycable_push_b | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment