Node.js Socket IO: How to continuously save socket data to MongoDB
<p>I'm building a 3D game in the browser using <code>THREE.js</code>. Lots of fun, but I came across the following situation:</p>

<p>An object in my 3D scene is continuously moving around, driven by user input. I need to save the object's position to my database in real-time.</p>

<p>Let's start at the <strong>front-end</strong>. <code>Angular.js</code> is watching my object's position using its built-in <code>$watch</code> functionality. The object's position can change <em>multiple times per second</em>.</p>

<p>On each change, I emit an event to the backend <code>Node.js</code> server using <code>Socket IO</code>, like so:</p>

<pre><code>socket.emit('update', { 
    id: id,
    position: position
});
</code></pre>

<p>In the <strong>back-end</strong>, the event is caught and immediatly emitted to other members in the same <code>Socket IO Room</code>. This way, everyone in this room will have the most real-time update possible.</p>

<p>Now, because the event can happen <em>multiple times per second</em>, I don't want to update my <code>MongoDB</code> collection on each change, since this would cause a lot of overhead. Instead, I'm looking for a way of <em>incidentally</em> saving data to the database.</p>

<p>I've came up with a solution by using Node.js <code>setInterval</code> function, which saves data every <code>1000ms</code>. For each distinct <code>id</code> (which is unique per object) received on the backend, a new key is created on an JavaScript object, thus keeping track of changes on a per-object basis.</p>

<p>The (simplified) code on the backend:</p>

<pre><code>let update_queue = new Object();

// ...

// Update Event
socket.on('update', (msg) =&gt; {

    // Flag Changes
    if (!update_queue[msg.id]) update_queue[msg.id] = { changes: true };

    // Set Interval Timer
    if (!update_queue[msg.id].timer) {

        update_queue[msg.id].timer = setInterval(() =&gt; {

            if (!update_queue[msg.id].changes) {
                clearInterval(update_queue[msg.id].timer);
                return;
            }

            // This saves data to MongoDB
            Object3DCollection.update(msg.id, msg.position)
                .then((res) =&gt; {
                    console.log('saved');
                });

            // Unflag Changes
            update_queue[msg.id].changes = false;

        }, 1000);
    }

    // Immediate Broadcast to Socket Room
    socket.broadcast.to('some_room').emit('object_updated', msg);
});
</code></pre>

<p><strong>The Question</strong></p>

<p>Is this a proper way of handling very frequent socket data and still saving it to a database? Or are there any other suggestions/solutions that are more robuust or work better.</p>

<p><strong>Note</strong>  </p>

<p><em>I do not want to wait for my object to be saved to the database and <strong>then</strong> emit the saved data to the rest of the socket room. The delay of database write operations is not suitable for the real-time game situation I'm dealing with.</em></p>

<p>Thanks in advance! All suggestions/solutions are appreciated and will be considered.</p>