I am using websockets to get data for a react-redux app I am creating, though I am currently sending executing the action the send a message, if not connected to the websocket, holding onto the message in an array then when the websocket connects (onopen
) I send all messages with Date.now()
of less than 10 mins ago.
Is this okay? Or is there a proper way to handle this sort of action.
Component
componentDidMount() {
this.props.actions.getData()
}
Socket Handler
let heldMessages = [];
/* ...Socket setup */
socket.onopen = () => {
if(heldMessages.length) {
heldMessages.forEach(msg => {
// In last 10 Minutes
if(msg.time > (Date.now() - 600000)) {
sendData(msg.data);
}
});
heldMessages = [];
}
}
/* ... misc functions */
function sendData(obj) {
if(connected) {
/* ...socket sending logic */
} else {
heldMessages.push({ time: Date.now(), data: obj });
}
}