Ancinek Ancinek - 13 days ago 4
React JSX Question

Rails with ReactJS - realtime data fetch

Can I fetch the data from the my database in realtime using Rails and ReactJS?

I have just started using React with Rails as this is pretty awesome combination. What I want to know is if there is a way to tell the view to update itself after a component is deleted/ created (so a record is removed/ added to the database)? Something like user after entering the page will subscribe to the database event (?).

What I have now is

ComponentContainer
with a custom fetch method that gets json from the server. I put it in the
componentWillMount
and inside a
setInterval
and run it every single second:

var ComponentsContainer = React.createClass({
componentWillMount() {
this.fetchRecords();
setInterval(this.fetchRecords, 1000);
},
fetchRecords() {
$.getJSON(path_to_results,
(data) => this.setState({components: data});
}, ...render methods etc.
});

Answer

Sounds like you might be looking for ActionCable, a Rails 5 thing. It uses sockets to provide server/client communication and will enable the server to send or broadcast messages to clients.

Comments