嘿, 我是Mofei!
Solution for Message Merging into One Due to High Frequency Messaging in Node.js Socket

Recently, during the implementation of a socket using node, I discovered something quite fascinating.

The process was as follows: we created a server using node and listened for the "data" event on that server, as shown below:

net.createServer(function(socket) {
    sock.on('data', function(data) {
        // After obtaining the data, perform JSON formatting
        JSON.parse(data)
    }
}).listen(port, ip)

After testing, we found everything was working fine. However, after a moment of joy, we suddenly received a bad news: this method would throw an error and terminate under high concurrency.

Upon inspection, we found that there was an issue with the handling of the data event. Normally, the data received in the data event should look like this:

{"data":"123"}

However, when we continuously sent requests to the socket, we noticed that as long as the interval between them was short enough, two messages would get merged together:

{"data":"123"}{"data":"123"}

At this point, performing JSON.parse would naturally throw an error.

We identified the cause of the problem, but how do we solve it?

We thought of a few solutions:

  1. Limit the interval time between individual data messages.
  2. Process the returned values.

The first method is simple and brute-force, by setting a certain interval for all send requests when sending. But! This method is absolutely anti-human.

So, through careful observation, we discovered a pattern. Since sockets follow the TCP protocol, the smallest unit received must be a complete JSON string, meaning we don’t have to worry about receiving a partial message. We can then focus on handling situations where multiple messages are combined together.

After experimenting with various methods, we adopted the following regex to handle it.

.match(/(\{.+?\})(?={|$)/g)

The key point is this regex “/(\{.+?\})(?={|$)/g”, which can match individual JSON strings. The technical term for this is “zero-width assertion”. I will publish a separate article on “zero-width assertion” later.

Once matched, the subsequent tasks become easier; you can handle each extracted JSON using any method you prefer.

THE END

More Articles You Might Be Interested In

I had so many questions while writing this—what’s your take?

avatar

Mofei's Friend (Click to edit)

The world is big, say something!

HI. I AM MOFEI!

NICE TO MEET YOU!