1

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.
});

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.