Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js to Socket.io Time Delay

I am working in real time trading application using Node.js(v0.12.4) and Socket.io(1.3.2). In that, I am facing some time delay nearly (100ms) when the response emitting from Node.js to GUI(Socket.Io).

I don't have a clue why the time delay is there while emitting data from Node.js to GUI (Socket.IO).

This happening in Production Site. And we tried to debug this in production server location also because of network latency. But same result.

Please anyone help me on this?

like image 909
MadTech Avatar asked Feb 02 '16 10:02

MadTech


1 Answers

One huge thing to note before doing the following. When calculating timing from back-end(server side) to front end (client side) you need to run this on the same computer that uses the same timing crystal.

quartz crystal-driven timing even on high quality motherboards deffer from one another.

If you find no delay when calculating time delay from back-end(server side) to front end (client side) on the same pc then the delay you originally found was caused by either the network connection or the deference in the motherboards timing crystals. Which would eliminate Node.js and Socket.io as the cause of the time delay.

Basically you need to find out where the delay is happening before you can solve the problem.

What you need to do is find out what is causing the largest performance hit in your project. In order to do this you will need to isolate the time each process takes. You also need to measure the time delay from the initial retrieval of the data to the release of the data. Then measure the time delay of each function, method and process. Try to isolate the problem. You need to ask what is taking the most time to do?

In order to find out where your performance hit is coming from you need to do the following.

  1. Find out how long it takes for your code to get the information it needs.
  2. Find out how long each information manipulation process takes before it sends out the data i.e function/methods...
  3. After the information is sent out find how long it takes to get the information to the client side and load.
  4. Add all of the times up and make sure it is equal to your performance delay in order to insure you have all the data you require to isolate the performance leak.
  5. Order every method, function, process… by its time delay most time consuming to least time consuming. When you find what processes are causing the largest delays you will then be able to fix the problem... Or at least explore tangible solutions...

Here are some tools you can use to measure the performance throughout your code.

First: Chrome has a really good tool that lets you see the performance of each piece of executed code.

  • https://developers.google.com/web/tools/chrome-devtools/profile/evaluate-performance/timeline-tool?hl=en
  • https://developer.chrome.com/devtools/docs/timeline

Second: performance-now node package. You can use it for dev performance testing.

Third: Stack overflow post on measuring time/performance in js.

Fourth: you can use things like console.time()

  • https://nodejs.org/api/console.html
  • https://developer.mozilla.org/en-US/docs/Web/API/Console/time
  • https://developer.chrome.com/devtools/docs/console-api
like image 72
Tim Avatar answered Oct 22 '22 13:10

Tim