Home Web Front-end JS Tutorial How do Turbo Streams Work (behind the scenes)

How do Turbo Streams Work (behind the scenes)

Oct 22, 2024 pm 11:40 PM

This article was originally published on Rails Designer


Turbo Streams allows you to update specific parts of your app upon a web request (controller action), referred to as just Turbo Streams. Or as a Turbo Stream Broadcasts when fired by your back end (on model create, update or destroy or manually from any object) over websockets, typically through ActionCable.

While the source is different, the response (HTML) for both are the same. I want to quickly explain how Turbo Streams work, so you understand that there is, just like with Rails, no magic involved ??. Just Plain Old JavaScript!

To broadcast a Turbo Stream you do something like this:

class Resource < ApplicationRecord
  after_create_commit -> { broadcast_append_to "resources" }
end
Copy after login
Copy after login

And for controller actions (or inline in the controller, if that's your jam):

<turbo-stream action="append" target="resources">
  <%= render @resource %>
</turbo-stream>
Copy after login
Copy after login

So what's the response (HTML) sent over for both options?

<turbo-stream action="append" target="resources">
  <template>
    <!-- HTML content of the Resource -->
  </template>
</turbo-stream>
Copy after login

That looks an awful lot like the Turbo Stream response you create for controller actions! The only big difference is the template-element wrapped around the HTML (coming from a partial or ViewComponent). The template-element is a container for holding HTML content that is hidden.

? You can see responses like these in your browser's devtools.

How do Turbo Streams Work (behind the scenes)

Once the turbo stream element is injected into the DOM, Turbo takes over. turbo-stream is nothing more than a custom element. It is defined here. You can see it in turn defines a connectedCallback() function. That function is called each time the element is added to the document; this is a feature of custom elements.

So what happens next? Let's go over the most important parts, step by step. Brace yourselves! ?️?

  1. a custom event, beforeRenderEvent, is dispatched;
  2. this event calls the renderElement function;
  3. then performAction is called;
  4. the action defined is then called.

In that last file, you can see all the supported, default, actions for Turbo Stream (append, prepend, replace, etc.). If you are, even a little bit, familiar with JavaScript, you should easily grasp what each separate action is doing (if not; check out JavaScript for Rails Developers ?). In essence, except for the remove action; grab the HTML from within the template-element and add it to the DOM (based on the action; append, prepend, after, etc.).

With that knowledge, you might see that you can just insert that custom turbo-stream element manually, and Turbo knows to pick it up.

class Resource < ApplicationRecord
  after_create_commit -> { broadcast_append_to "resources" }
end
Copy after login
Copy after login

Just copy above HTML and view it in the browser. You will see the li-element being appended to the ul-element. ? Then using your browser's dev-tools, paste another turbo-stream element anywhere in the DOM:

<turbo-stream action="append" target="resources">
  <%= render @resource %>
</turbo-stream>
Copy after login
Copy after login

Pretty cool, right? Turbo uses many features from the browser to give that smooth developer experience we all love. Now you know how Turbo Stream works behind the scenes!

The above is the detailed content of How do Turbo Streams Work (behind the scenes). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

HTTP Debugging with Node and http-console

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

See all articles