DEV Community

Cover image for Proxy in Typescript
Umme Q
Umme Q

Posted on

4 1

Proxy in Typescript

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.

The proxy pattern is useful when the real subject does something expensive.
In web applications, one of the most expensive operations you can do is a network request, so it makes sense to combine HTTP requests as much as possible.

Real Example as:

You have a list of videos on the page. When the user clicks a video title, the area below the title expands to show more information about the video and also enables the video to be played.

The detailed video information and the URL of the video are not part of the page; they need to be retrieved by making a web service call.

The web service can accept multiple video IDs, so we can speed up the application by making fewer HTTP requests whenever possible and retrieving data for several videos at one time.

The videos object doesn't call the HTTP service directly but calls the proxy instead.

The proxy then waits before forwarding the request. If other calls from videos come in the 50ms waiting period, they will be merged into one.

A delay of 50ms is pretty much imperceptible for the user but can help combine requests and speed up the experience when clicking “toggle” and expanding more than one video at once.

It also reduces the server load significantly since the web server has to handle a smaller number of requests.

Request process without proxy
Alt Text

Request process with proxy
Alt Text

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay