diff --git a/examples/hello-world/Gemfile b/examples/hello-world/Gemfile new file mode 100644 index 0000000..27d6a9d --- /dev/null +++ b/examples/hello-world/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'puma' +gem 'rack' +# gem 'datastar' +gem 'datastar', path: '../../../sdk/ruby' diff --git a/examples/hello-world/Gemfile.lock b/examples/hello-world/Gemfile.lock new file mode 100644 index 0000000..cdb417b --- /dev/null +++ b/examples/hello-world/Gemfile.lock @@ -0,0 +1,29 @@ +PATH + remote: ../../../sdk/ruby + specs: + datastar (1.0.0.beta.3) + json + logger + rack (>= 3.1.14) + +GEM + remote: https://rubygems.org/ + specs: + json (2.12.2) + logger (1.7.0) + nio4r (2.7.4) + puma (6.6.0) + nio4r (~> 2.0) + rack (3.1.16) + +PLATFORMS + arm64-darwin-24 + ruby + +DEPENDENCIES + datastar! + puma + rack + +BUNDLED WITH + 2.6.3 diff --git a/examples/hello-world/hello-world.html b/examples/hello-world/hello-world.html new file mode 100644 index 0000000..2fd7e90 --- /dev/null +++ b/examples/hello-world/hello-world.html @@ -0,0 +1,35 @@ + + + + + + Datastar SDK Demo + + + + +
+
+

+ Datastar SDK Demo +

+ Rocket +
+

+ SSE events will be streamed from the backend to the frontend. +

+
+ + +
+ +
+
+
Hello, world!
+
+ + \ No newline at end of file diff --git a/examples/hello-world/hello-world.ru b/examples/hello-world/hello-world.ru new file mode 100644 index 0000000..bda9957 --- /dev/null +++ b/examples/hello-world/hello-world.ru @@ -0,0 +1,37 @@ +require 'bundler/setup' + +require 'datastar' + +# This is a test Rack endpoint +# with a hello world example using Datastar. +# To run: +# +# # install dependencies +# bundle install +# # run this endpoint with Puma server +# bundle exec puma ./hello-world.ru +# +# Then open http://localhost:9292 +# +HTML = File.read(File.expand_path('hello-world.html', __dir__)) + +run do |env| + datastar = Datastar.from_rack_env(env) + + if datastar.sse? + delay = (datastar.signals['delay'] || 0).to_i + delay /= 1000.0 if delay.positive? + message = 'Hello, world!' + + datastar.stream do |sse| + message.size.times do |i| + sse.patch_elements(%(
#{message[0..i]}
)) + sleep delay + end + end + else + [200, { 'content-type' => 'text/html' }, [HTML]] + end +end + +trap('INT') { exit } diff --git a/examples/threads/Gemfile b/examples/threads/Gemfile new file mode 100644 index 0000000..27d6a9d --- /dev/null +++ b/examples/threads/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'puma' +gem 'rack' +# gem 'datastar' +gem 'datastar', path: '../../../sdk/ruby' diff --git a/examples/threads/Gemfile.lock b/examples/threads/Gemfile.lock new file mode 100644 index 0000000..cdb417b --- /dev/null +++ b/examples/threads/Gemfile.lock @@ -0,0 +1,29 @@ +PATH + remote: ../../../sdk/ruby + specs: + datastar (1.0.0.beta.3) + json + logger + rack (>= 3.1.14) + +GEM + remote: https://rubygems.org/ + specs: + json (2.12.2) + logger (1.7.0) + nio4r (2.7.4) + puma (6.6.0) + nio4r (~> 2.0) + rack (3.1.16) + +PLATFORMS + arm64-darwin-24 + ruby + +DEPENDENCIES + datastar! + puma + rack + +BUNDLED WITH + 2.6.3 diff --git a/examples/threads/threads.ru b/examples/threads/threads.ru new file mode 100644 index 0000000..2a14bfc --- /dev/null +++ b/examples/threads/threads.ru @@ -0,0 +1,84 @@ +require 'bundler/setup' + +require 'datastar' + +# This is a test Rack endpoint +# to demo streaming Datastar updates from multiple threads. +# To run: +# +# # install dependencies +# bundle install +# # run this endpoint with Puma server +# bundle exec puma threads.ru +# +# visit http://localhost:9292 +# +INDEX = <<~HTML + + + + + Datastar counter + + + + + Start +

Slow thread: waiting

+

Fast thread: waiting

+

Disconnected...

+ + +HTML + +trap('INT') { exit } + +run do |env| + # Initialize Datastar with callbacks + datastar = Datastar + .from_rack_env(env) + .on_connect do |sse| + sse.patch_elements(%(

Connected...

)) + p ['connect', sse] + end.on_server_disconnect do |sse| + sse.patch_elements(%(

Done...

)) + p ['server disconnect', sse] + end.on_client_disconnect do |socket| + p ['client disconnect', socket] + end.on_error do |error| + p ['exception', error] + puts error.backtrace.join("\n") + end + + if datastar.sse? + # This will run in its own thread / fiber + datastar.stream do |sse| + 11.times do |i| + sleep 1 + # Raising an error to demonstrate error handling + # raise ArgumentError, 'This is an error' if i > 5 + + sse.patch_elements(%(#{i})) + end + end + + # Another thread / fiber + datastar.stream do |sse| + 1000.times do |i| + sleep 0.01 + sse.patch_elements(%(#{i})) + end + end + else + [200, { 'content-type' => 'text/html' }, [INDEX]] + end +end