2018-12-24 02:06:22 +00:00
**µWebSockets.js** is a JavaScript platform, runtime and web server built on [µWebSockets ](https://github.com/uNetworking/uWebSockets ) v0.15 and V8.
2018-11-04 23:00:00 +00:00
There are two modes; compiled as a stand-alone JavaScript runtime or as a Node.js native addon.
2018-07-28 20:48:22 +00:00
2018-12-26 08:15:31 +00:00
For the most common Node.js systems are available precompiled binaries:
```
2019-01-08 21:25:03 +00:00
npm install uNetworking/uWebSockets.js#semver:0.0.1
2018-12-26 08:15:31 +00:00
```
or any such version.
2018-07-28 20:48:22 +00:00
```javascript
2018-11-04 23:00:00 +00:00
/* The stand-alone runtime has uWS namespace already loaded. */
var uWS = uWS ? uWS : require('../dist/uws.js');
const world = 'Strings are slower than ArrayBuffer but who cares for demo purose!';
const port = 3000;
2018-07-28 20:48:22 +00:00
2018-11-04 23:00:00 +00:00
uWS.App().get('/hello', (res, req) => {
res.end(world);
2018-10-04 21:18:50 +00:00
}).get('/*', (res, req) => {
2018-11-04 23:00:00 +00:00
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
}).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
### Benchmarks
2018-12-26 14:24:15 +00:00
Performance retention is up to 75% of native C++ [µWebSockets ](https://github.com/uNetworking/uWebSockets ) v0.15. That puts it some 20x as fast as Deno and even faster than most C++-only servers, all from within a JavaScript VM.
2018-11-30 03:32:42 +00:00
![](https://github.com/uNetworking/uWebSockets/blob/master/misc/bigshot_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
m ake
node examples/HelloWorld.js
```