Hasty Briefsbeta

How does Turbo listen for Turbo Streams

6 days ago
  • #DOM Manipulation
  • #Rails
  • #Turbo Streams
  • Turbo Stream elements are custom HTML elements that change the DOM when added.
  • A <turbo-stream> tag can trigger seven DOM-changing actions: append, prepend, replace, update, remove, before, and after.
  • Turbo intercepts form submissions, adds a custom MIME type to the 'Accept' header, and expects Turbo Stream responses.
  • The server must respond with Turbo Stream elements or issue a redirect; otherwise, Turbo throws an error.
  • Turbo listens for responses with 'Content-Type: text/vnd.turbo-stream.html' and adds them to the DOM, triggering actions.
  • Multiple <turbo-stream> elements can be included in a single response, executing actions sequentially.
  • Turbo Streams can also be sent via WebSockets without client requests.
  • The @rails/request.js library simplifies handling Turbo Stream responses in Rails applications.