How do I constantly update my ReactJS client from my Ruby REST / CRUD API without polling?

BACKGROUND

I am creating a simple ReactJS app as a proof of concept; with the Flux architecture and all.

I have a Ruby and MongoDB API built using Sinatra and Mongoid .

The front-end element is statically used with ReactJS , JSX and Fluxxor .

I am currently retrieving the state of the application using a click handler that does the following function:

function(callback){
  var the_url = (this.state.isSelected) ? '/disable' : '/enable' ;
  $.ajax({
    url: the_url,
    dataType: 'json',
    cache: false,
    success: function(data) {
      callback(data);
    }.bind(this),
    error: function(xhr, status, err) {
      console.error(the_url, status, err.toString());
    }.bind(this)
  });
}

      

This is a simple AJAX call. I understand that I could achieve some sort of real-time by refactoring a bit and having a piece of code like the one above run periodically; basically a survey .

I read and said that the "true way to react" to doing things would be if the back end notifies the front-end when the application state has changed and that the way to achieve this is to use WebSockets securely .

Realistic

Given the stack I described , what libraries should I use to handle these sockets at either end of the application, and what is the simplest possible implementation?

+3


source to share


1 answer


Your decision doesn't depend on how you use React, you should use the same way as in real time, as if you were using any foreground technique. The problem with WebSockets is basically that browser support was a bit erratic, socket.io is a pretty popular way to get around this.



Socket.io is mostly focused on Node.js, but should be workable from any backend. Here is a post using Rails.

0


source







All Articles