Getting lag in http service

i am using raspberry pi 4 to make a local video steaming server and accessing it remotely using remoteit but there is a lag in video about 4 to 5 second how can i resolve this lag. On local network there is no lag.

There are times where the lag is caused by the latency over the internet itself or server configuration. This can be due to the number of hops from the device itself to the client. Can you explain more about the type of webserver, type of compression used for video streaming, location of the web server and location of the client?

Simple Mjpeg steam is used for video steaming. Both client and server are connected to my home network.
this example is used to make Picamera a as local server.

How are you connecting to your pi/camera? If your connecting via the web portal, then the data has to go to the cloud and back and is going to limit your performance. If your connecting via the desktop then you should get a direct connection on your network which should be fast.

It should be noted that the MJPEG stream is not optimal for remote access, so your going to get some latency on the viewing end. It been my experience with remote viewing of cameras with minimum latency page flipping jpgs with javascript provides optimal latency that adapts for almost any bandwidth.

But viewing remotely most cameras can have 1-10 seconds latency when viewing remotely. Even my professional cameras have this.