Sending and Receiving Messages
Exchange data with send and onmessage.
Sending Text
Once open, socket.send(string) transmits a text message to the server.
socket.onopen = () => {
socket.send("ping");
};Receiving with onmessage
onmessage fires for every incoming frame. The data is on event.data.
socket.onmessage = (event) => {
console.log("Received:", event.data);
};All lessons in this course
- Opening a WebSocket Connection
- Sending and Receiving Messages
- Connection Lifecycle and Errors
- Reconnection Strategies