

Rails 7 Adds ability to handle reconnects with the connected callback
source link: https://blog.saeloun.com/2023/02/10/rails-add-reconnects-with-connected
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is Action cable
Action Cable is a feature of the Ruby on Rails framework that provides real-time, bidirectional communication between a web server and its clients using WebSockets. It allows for the creation of dynamic, interactive user experiences by enabling real-time updates in the browser without the need for a page refresh.
Action Cable works by setting up a persistent connection between the client (typically a web browser) and the server. The client can send messages to the server and receive messages from the server in real-time, which enables real-time updates in the user interface. It provides both client-side JavaScript framework and a server-side Ruby framework.
Create a Client side channel subscription
// app/javascript/channels/chatroom_channel.js
import consumer from "./consumer"
consumer.subscriptions.create({ channel: "ChatRoom", room_id: room_id}, {
connected() {
// Called when the subscription is ready for use on the server
console.log('connected to ChatRoom', room_id)
},
disconnected() {
// Called when the subscription has been terminated by the server
},
received(data) {
// Called when there's incoming data on the websocket for this channel
console.log(data)
}
});
Before
Considering the above example, In the previous versions of Rails,
the connected method is called when the WebSockets connection is established, and the disconnected method is called when the connection is lost
After
Rails 7 has added the ability for subscribers to handle reconnections with the
connected()
callback if they miss some messages due to a lost connection.
// app/javascript/channels/chatroom_channel.js
import consumer from "./consumer"
consumer.subscriptions.create({ channel: "ChatRoom", room_id: room_id}, {
connected({reconnected}) {
// Called when the subscription is ready for use on the server
console.log('connected to ChatRoom', room_id)
if (reconnected) {
console.log('reconnected to ChatRoom', room_id)
}
}
});
Check out the PR for more details.
Recommend
-
11
Google Photos adds the ability to make a 2D photo a 3D "Cinematic" representation
-
8
Rails 7 adds the ability to schedule the query on the background thread pool Mar 31, 2021 , by Swaathi Kakarla 2 minute read
-
5
ActiveJob is used to enqueue and run the tasks in the background. But a job can fail because of many reasons. Say a network issue while communicating to the underlying datastore or non-availability of the datastore, etc. ActiveJob
-
17
A lot of times when working on web applications, a need arises to redirect a user without the user explicitly clicking on a link. For example, assume that a user has just created a post on an application. Once the post is created, it makes se...
-
5
For a very long time in Rails, it was not possible to combine delete statements with GROUP_BY and HAVING queries. It does seem like something that should have been supported out of the box, but unfortunately was overlooked — until now!
-
6
Ahmad khattab Posted on Jan 15...
-
5
Rails adds the ability to prevent unwarranted shard swapping Mar 29, 2022 , by Swaathi Kakarla 2 minute read
-
8
Saturday, September 17, 2022 Handling reconnects in Action Cable, no more Coffeescript and a new guide. Posted by four54 Hi, this is
-
5
Meta Adds Enhancements for Call Ads, Including New Callback Option Published Sept. 28, 2022 By
-
6
Rails adds the ability to ignore tables in SQL schema dumps via regular expressions Jan 10, 2023 , by Prasanth Chaduvula 1...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK