Skip to content

Instantly share code, notes, and snippets.

@chainhead
Last active March 24, 2021 05:00
Show Gist options
  • Save chainhead/0570a95f47032ca0b55295aa8b0d5544 to your computer and use it in GitHub Desktop.
Save chainhead/0570a95f47032ca0b55295aa8b0d5544 to your computer and use it in GitHub Desktop.
HTTP server handling connection upgrade

Introduction

This gist shows a basic implementation of a HTTP server that handles the request to upgrade connection to Websockets.

const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
server.on('upgrade', (request, socket, head) => {
wss.handleUpgrade(request, socket, head, function done(w) {
wss.emit('connection', ws, request)
})
})
server.listen(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment