uWebSockets.js/README.md

67 lines
2.7 KiB
Markdown
Raw Normal View History

2019-01-15 13:33:43 +00:00
<div align="center">
<img src="misc/logo.svg" height="180" />
2018-11-04 23:00:00 +00:00
2019-01-15 13:33:43 +00:00
*µWebSockets™ (it's "[micro](https://en.wikipedia.org/wiki/Micro-)") is simple, secure & standards compliant web I/O for the most demanding*<sup>[[1]](benchmarks)</sup> *of applications.*
2018-07-28 20:48:22 +00:00
2019-01-15 13:33:43 +00:00
• [Read more](misc/READMORE.md)
</div>
### NPM install
2019-01-15 14:08:48 +00:00
While not physically hosted by NPM, it's still possible to install using NPM tools like so ([npm docs](https://docs.npmjs.com/cli/install)):
2018-12-26 08:15:31 +00:00
```
2019-01-15 14:08:48 +00:00
npm install uNetworking/uWebSockets.js#binaries
2018-12-26 08:15:31 +00:00
```
2019-01-15 13:33:43 +00:00
### Presenting; faster I/O for Node.js
Every other week a new "web framework" pops up for Node.js. Fastify, Restana, Aero, Micro and so on. Most aim to improve I/O performance, but fall flat. You can't fix an inherent design flaw of an entire stack just by slapping a few lines of JavaScript on top.
2018-12-26 08:15:31 +00:00
2019-01-15 13:33:43 +00:00
µWS.js is a 100% C/C++ web platform completely bypassing the entire Node.js I/O stack. JavaScript-exposed functions and objects are entirely backed by native code all the way down to the OS kernel. This makes it possible to reach unprecedented I/O performance from within Node.js (or any stand-alone V8 build). Scroll down for benchmarks.
2018-11-04 23:00:00 +00:00
2019-01-15 13:33:43 +00:00
##### In a nutshell
```javascript
const uWS = require('uWebSockets.js');
2018-11-04 23:00:00 +00:00
const port = 3000;
2018-07-28 20:48:22 +00:00
2019-01-15 13:33:43 +00:00
uWS.SSLApp({
/* cert, key and such specified here, or use uWS.App */
}).get('/whatsmyuseragent', (res, req) => {
res.writeHeader(
'content-type', 'text/html; charset= utf-8'
).end(
'Your user agent is: ' + req.getHeader('user-agent')
);
2018-10-04 21:18:50 +00:00
}).get('/*', (res, req) => {
2019-01-15 13:33:43 +00:00
res.end('Wildcard route');
2018-11-04 23:00:00 +00:00
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
2018-10-04 21:18:50 +00:00
});
2018-07-28 20:48:22 +00:00
```
2018-11-03 11:45:56 +00:00
2019-01-15 13:33:43 +00:00
### Streams
Proper streaming of huge data is supported over Http/Https and demonstrated with examples. Here's a shot of me watching real-time streamed HD video from Node.js while simultaneously scoring a 115k req/sec with wrk. For my computer, that's about 5x that of vanilla Node.js (without any HD video streaming/playing).
![](misc/streaming.png)
2018-11-03 11:45:56 +00:00
### Benchmarks
2019-01-15 13:33:43 +00:00
Performance retention is about 65% that of the native C++ [µWebSockets](https://github.com/uNetworking/uWebSockets) v0.15. That makes it some 20x as fast as Deno and even faster than most C++-only servers, all from within a JavaScript VM.
Http | WebSockets
--- | ---
![](https://github.com/uNetworking/uWebSockets/blob/master/misc/bigshot_lineup.png) | ![](https://github.com/uNetworking/uWebSockets/blob/master/misc/websocket_lineup.png)
2018-11-03 23:17:07 +00:00
2018-12-26 08:15:31 +00:00
### Build from source
2018-11-04 23:00:00 +00:00
Easiest is to compile yourself a Node.js native addon. The following works for Linux and macOS systems:
2018-11-03 23:17:07 +00:00
```
2018-11-04 23:00:00 +00:00
git clone --recursive https://github.com/uNetworking/uWebSockets.js.git
cd uWebSockets.js
2018-11-03 23:17:07 +00:00
make
node examples/HelloWorld.js
```