top of page

Rails Turbo 8



Turbo 8 is a Ruby on Rails feature that facilitates automatic broadcasting of data transmissions, enhancing the real-time capabilities of Rails applications. It leverages WebSockets for efficient communication between the server and client, enabling seamless updates to the user interface without requiring manual page reloads.


Turbo 8 establishes a persistent connection between the client and server, enabling bidirectional data flow. When a relevant data change occurs on the server side, Turbo 8 automatically pushes these updates to connected clients, ensuring that the user interface reflects the latest state of the application in real-time.

This broadcasting mechanism significantly improves the responsiveness and interactivity of Rails applications, making them more dynamic and engaging for users. By eliminating the need for frequent page refreshes, Turbo 8 enhances the user experience and simplifies development, as developers can focus on building functionality without worrying about manual data synchronization between the server and client.


Create Project

rails new watching_movies -T

Add additional gems

bundle add tailwindcss-rails
rails tailwindcss:install
gem install foreman
bundle add faker

scaffold Movie

rails g scaffold movie title watched_at:datetime

In the model, put the magic

class Movie < ApplicationRecord
	broadcasts_refreshes # update and destroy take care of this
	after_create :broadcast_create
	private

	def broadcast_create
		broadcast_prepend_to self, target: "movies", partial: "movies/movie", locals: { movie: self } 
  end
end

At the top of the page movies/_movie.html.erb

<%= turbo_stream_from movie %>

In the index.html.erb will have this code

<div id="movies" class="min-w-full">
	<%= turbo_stream_from :movies %>
	<%= render @movies %>
</div>

Why the turbo_stream_from :movies? The application needs to add the new movie partial at the top. So in the movies_controller.rb change to

def index
    @movies = Movie.order(created_at: :desc)
end

We need to prepare the database

In file db/seeds.rb add

10.times do
	Movie.create(title: Faker::Movie.title)
end
rails db:drop db:create db:migrate db:seed

This setup demonstrates how Turbo 8 can be integrated into a Rails application to enable automatic broadcasting of data updates to connected clients, enhancing the real-time capabilities of the application.

Posts recentes

Ver tudo

Comments


Captura de tela de 2024-01-01 22-06-25.png

Hi, I'm Rodrigo Toledo

A full-stack developer skilled in both front-end and back-end development, building and maintaining web applications

  • Facebook
  • Youtube
  • LinkedIn
  • Instagram

I don't know everything, help me

Every day, I try to improve what I know about frameworks, and when this occurs, I'll try to share it with the community. Every day, I try to improve my knowledge about frameworks. When this happens, I will try to share it with the community.

Subscribe

Thanks for submitting!

bottom of page