Websockets
Standard Protocol
A WebSocket is a standard protocol for two-way data transfer between a client and server. The WebSockets protocol does not run over HTTP, instead it is a separate implementation on top of TCP.
What is the use of Websocket ?
A WebSocket connection allows full-duplex communication between a client and server so that either side can push data to the other through an established connection. The reason why WebSockets, along with the related technologies of Server-sent Events (SSE) and WebRTC data channels, are important is that HTTP is not meant for keeping open a connection for the server to frequently push data to a web browser. Previously, most web applications would implement long polling via frequent Asynchronous JavaScript and XML (AJAX) requests.
Real-time Usage of Websockets:
- Collaborative coding: Working on a copy of a document used to suffice, but then you had to figure out a way to merge all the edited copies together. Version control systems like Git can help with certain files, but you’ll still have to track people down when Git finds a conflict it can’t deal with. With a collaborative solution like WebSockets, we can work on the same document and skip all the merges.
- Sports Updates (Really) ? I want to do is add latency to user experience. If you’re including sports information in your Web app, WebSockets can keep your users up to speed. Triggering the latency can really improve the user experience of the application .
- Location — tracking applications: If you’re tracking user locations over time (such as running an app that tracks your progress along a route), you’ll be collecting fine-grained data. If you want to update a Web dashboard in real time (say, a track coach monitoring the progress of badminton players) HTTP is going to be unnecessarily bulky. Leverage the TCP connection a WebSocket uses and let that data fly.
Best practices of Websockets:
- Check your network topology of your applicaton:Websockets are designed for communication between two devices that are connected to the same network. For example, an application running on a phone might need to communicate with an application running on a server in a different country.If you’re using websockets in this type of scenario, it’s important to consider how the different networks will interact with each other. In particular, you need to be aware of potential security risks that could arise from connecting different networks.
- Keep eye on Memory Usage: Websockets are constantly sending and receiving data, which means they can quickly use up a lot of memory. If you’re not careful, this can lead to your server running out of memory and crashing.. There are a few ways to do this, but one of the simplest is to use the ws module’s built-in memoryUsage() function.This function will return an object containing information about the current memory usage of your websocket connection. You can then use this information to decide whether or not you need to take action to reduce memory usage.
- Secure Websockets: WebSockets are a duplex communication protocol, meaning they allow for full-duplex, or two-way, communication. This is in contrast to the half-duplex, or one-way, communication of HTTP. Because WebSockets allow for two-way communication, they open up a whole new world of potential attacks that didn’t exist with HTTP.