How to use web socket
- Create a new WebSockets connection
const connection = new WebSocket(url)
Method of WebSocket object
- onopen
When the connection is successfully established, the open event is fired.Listen for it by assigning a callback function to the onopen property of the connection object:
connection.onopen = () => {
//...
}
- onerror
connection.onerror = error => {
console.log(`WebSocket error: ${error}`)
}
- Send
connection.onopen = () => {
connection.send('hey')
}
- Receive
connection.onmessage = e => {
console.log(e.data)}
}
Comments
Post a Comment