Ancinek Ancinek - 1 year ago 89
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

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download