
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
PWS gives you a reconnecting websocket to use in the browser or in node simply by switching out new WebSocket with new PersistentWebSocket.
It behaves the same as a regular browser WebSocket, but reconnects automatically with a simple backoff algorithm if the connection closes.
const pws = new PersistentWebSocket(url)
// Called every time a connection is established
pws.onopen = () => pws.send('Hello')
// Echo messages received
pws.onmessage = event => pws.send('You said: ' + event.data)
More details at https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket
You can also use PWS with the nodejs WebSocket library ws
const WebSocket = require('ws')
, Pws = require('pws')
const pws = Pws(url, WebSocket)
// as in the browser...
More details at https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options
To ensure a persistent connection it's necessary to send messages at regular intervals from the server to keep the connection alive. The WebSocket protocol only implements a ping to be sent from the server, but not in the other direction. This can leave the client in a half open state where it thinks it's connected, but doesn't receive messages from the server. To prevent this state PWS let's you set a specific timeout after which to force a reconnection if you did not receive any messages from the server.
new PersistentWebSocket(url, {
pingTimeout: 30 * 1000 // Reconnect if no message received in 30s.
})
The backoff algorithm is inspired by primus and http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html, and stops at a maximum reconnection timeout of 5 minutes.
onlinePWS will also reconnect on the browsers online event, irregardless of the current timeout for the next reconnect, to ensure a connection is regained as fast as possible.
FAQs
A Persistent WebSocket wrapper
The npm package pws receives a total of 2,538 weekly downloads. As such, pws popularity was classified as popular.
We found that pws demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.