
Research
Security News
The Landscape of Malicious Open Source Packages: 2025 Mid‑Year Threat Report
A look at the top trends in how threat actors are weaponizing open source packages to deliver malware and persist across the software supply chain.
react-native-websocket
Advanced tools
Supply Chain Security
Vulnerability
Quality
Maintenance
License
WebSocket API wrapped as a component for React Native
If you're interested in using websockets in React Native generally here is a slightly abbreviated version of the source of this component:
class WS extends Component {
//...
send = (data) => this.state.ws.send(data)
componentDidMount () {
this.reconnect = !!this.props.reconnect
this._handleWebSocketSetup()
}
componentWillUnmount () {
this.reconnect = false
this.state.ws.close()
}
_handleWebSocketSetup = () => {
const ws = new WebSocket(this.props.url)
ws.onopen = () => {
this.props.onOpen && this.props.onOpen()
}
ws.onmessage = (event) => { this.props.onMessage && this.props.onMessage(event) }
ws.onerror = (error) => { this.props.onError && this.props.onError(error) }
ws.onclose = () => this.reconnect ? this._handleWebSocketSetup() : (this.props.onClose && this.props.onClose())
this.setState({ws})
}
}
As you can see the component simply wraps the native websocket api. It's also recommended that you implement your own exponential backoff reconnect logic if you plan on using this component in production.
$ npm install --save react-native-websocket
# OR
$ yarn add react-native-websocket
import React, { Component } from 'react'
import { AppRegistry, View } from 'react-native'
import WS from 'react-native-websocket'
export default class Example extends Component {
render () {
return (
<View style={{flex: 1}}>
<WS
ref={ref => {this.ws = ref}}
url="wss://echo.websocket.org/"
onOpen={() => {
console.log('Open!')
this.ws.send('Hello')
}}
onMessage={console.log}
onError={console.log}
onClose={console.log}
reconnect // Will try to reconnect onClose
/>
</View>
)
}
}
Contributions are welcome. Please open up an issue or create PR if you would like to help out.
Licensed under the MIT License.
FAQs
WebSocket API wrapped as a component for React Native
The npm package react-native-websocket receives a total of 2,196 weekly downloads. As such, react-native-websocket popularity was classified as popular.
We found that react-native-websocket 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.
Research
Security News
A look at the top trends in how threat actors are weaponizing open source packages to deliver malware and persist across the software supply chain.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.