Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created November 16, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Srushtika/be1ad08c3142c1015e4942e0233583b8 to your computer and use it in GitHub Desktop.
Save Srushtika/be1ad08c3142c1015e4942e0233583b8 to your computer and use it in GitHub Desktop.
WebSockets server tutorial
// Establish a WebSocket connection to the echo server
const ws = new WebSocket('wss://echo.websocket.org');
// Add a listener that will be triggered when the WebSocket is ready to use
ws.addEventListener('open', () => {
const message = 'Hello!';
console.log('Sending:', message);
// Send the message to the WebSocket server
ws.send(message);
});
// Add a listener in order to process WebSocket messages received from the server
ws.addEventListener('message', event => {
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment