Reputation: 55
I am trying to implement communication with server through websocket. Let's say that I can't use Flux or similar libraries. My problem is updating data on server response.
I am not sure how to implement callback functions on server response to update data.
So far I have something like this(not an actual code)
events.js
const e = require('event-emitter');
var events = e();
module.exports = events;
socket.js
const events = require('./events');
module.exports = {
var ws;
init: function(URL) {
ws = new WebSocket(URL);
ws.onopen = function() { ... }
ws.onclose = function() { ... }
ws.onmessage = function(data) {
events.emit(data.action, data);
}
},
send: function(data) {
ws.send(data);
}
}
model.js
const events = require('./events')
var data = [];
/* listening for some specific event from socket */
events.on("action", doAction);
function doAction(data) {
events.push(data);
/* now I have to emit event which react component
is listening to so it can update data */
events.emit("viewAction");
}
}
/* what react component uses to get and render data */
module.exports = {
get: () => data,
events: events
}
component.js
const React = require('react');
const ReactDOM = require('react-dom')
const model = require('./model');
var App = React.createClass({
componentDidMount: function() {
model.events.on('viewAction', this.refresh);
},
componentWillUnmount: function() {
model.events.off('viewAction', this.refresh);
}
render: function() {
return (
<div>
{model.get()}
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
I hope sample makes it clear what I am trying to do and what my problem is. This also makes model event listener not work unless it is included in one of the react components which I am not too happy about.
Is there a better way?
Upvotes: 3
Views: 1737
Reputation: 51826
What you have here is a simplistic implementation of the flux architecture:
In your case, your action comes from a websocket server response, your dispatcher is the websocket, and your store is the model.
A couple of points to improve your design further:
this.refresh
in the component save model.get()
to its state using this.setState()
so that it internally invokes re-rendering.shouldComponentUpdate()
in your component, and compare the old state to the current one in order to determine whether the component really needs to update. This improves the efficiency of your application dramatically, reducing the amount of reflows in your documentGood luck on whatever you're writing!
Upvotes: 1