
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
react-native-reconnecting-websocket
Advanced tools
React native auto reconnect websocket extends WebSocketModule. API design referenced reconnecting-websocket.
🇨🇳中文文档
It is API compatible, so when you have:
var ws = new WebSocket('ws://....');
you can replace with:
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'
var ws = new ReconnectingWebSocket('ws://....');
Less code, more exponential.
npm i react-native-reconnecting-websocket
With the standard WebSocket API, the events you receive from the WebSocket instance are typically:
onopen
onmessage
onmessage
onmessage
onclose // At this point the WebSocket instance is dead.
With a ReconnectingWebSocket, after an onclose event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
This is all handled automatically for you by the library.
var socket = new ReconnectingWebSocket(url, protocols, options);
url
protocols
options
Options can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:
var socket = new ReconnectingWebSocket(url, null, {reconnectInterval: 3000});
reconnectInterval
integer
1000
maxReconnectInterval
integer
30000
reconnectDecay
integer
or float
1.5
timeoutInterval
integer
2000
maxReconnectAttempts
integer
or null
.null
origin
origin
optionheaders
origin
as a WebSocket connection option is deprecated. Include it under headers
insteadorigin
and Cookie
WebSocket(url, '', { headers: { Cookie: 'key=value', origin: "https://secret-host.com" } });
ws.close(code?: number, reason?: string)
code
is optional the closing code (default value 1000). https://tools.ietf.org/html/rfc6455#section-7.4.1reason
is the optional reason that the socket is being closed. https://tools.ietf.org/html/rfc6455#section-7.1.6ws.send(data: string | ArrayBuffer | ArrayBufferView | Blob)
ws.ping()
How to add heartbeat?
ws.reconnect()
See the more detail in [react-native/WebSocket.js@3982a2c6]
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'
const ws = new ReconnectingWebSocket("ws://...");
// ws listeners
ws.onopen = (e) => {
console.log("onopen",e)
};
ws.onmessage = (evt) => {
console.log("onmessage",JSON.parse(evt.data))
};
ws.onclose = (e) => {
console.log("onclose",e)
};
ws.onerror = (e) => {
console.log("onerror",e)
};
/*
* listen reconnecting event (Powered by ReconnectingWebSocket)
* @params reconnectAttempts 尝试重连的次数
*/
ws.onconnecting = (reconnectAttempts) => {
console.log("onconnecting", reconnectAttempts)
}
ws.onopen = (e) => {
// execute immediately!
ws.send("heartbeat string");
heartCheck.reset().start()
};
ws.onmessage = (evt) => {
heartCheck.reset().start()
};
ws.onclose = () => {
if(logout){//如果前端主动关闭(如退出登陆),不做处理
}else {//如果服务器主动关闭,前端也需要重连时使用
heartCheck.reset().start()
}
};
var heartCheck = {
timeout: 10000,//default 10s
timeoutObj: null,
serverTimeoutObj: null,
reset:function(){
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
start:function(){
let self = this;
this.timeoutObj = setTimeout(function(){
if(ws.readyState === ws.OPEN){
ws.send("heartbeat string");
}
self.serverTimeoutObj = setTimeout(function(){
ws.reconnect();//本库提供
}, self.timeout)
}, this.timeout)
}
}
FAQs
websocket module
We found that react-native-reconnecting-websocket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.