uWebSockets.js

用于 Node.js 后端的 μWebSockets。「μWebSockets for Node.js back-ends 🤘」

Github stars Tracking Chart


简单、安全[1] 和符合标准的[2] web 服务器,适用于最苛刻[3] 的应用。 阅读更多...

很简单性能好

µWebSockets.js 是 Node.js 的 web 服务器旁路,它以高度优化的 C++ 语言重新实现了事件、网络、加密、网络协议、路由和 pub/sub。因此,µWebSockets.js 为 Node.js 提供的 web 服务是 Fastify 的 8.5 倍,是 Socket.IO 的至少 10 倍。它也是 Bun 的内置 web 服务器

µWebSockets.js 是 Node.js 的 WebSocket/HTTP 协议的 C++ 实现,易于在 JavaScript 中使用。可以把它看作是 Socket.IO 和 Fastify/Express.js 的一个更快的替代品;它同时支持路由器和 pub/sub。

  • 为简单起见,我们建议使用 bun install uNetworking/uWebSockets.js#v20.31.0 或其他类似版本进行安装。使用 Node.js LTS 的官方构建版本。
  • 浏览文档并查看主仓库。这有大量的例子,但要点如下:
/* Non-SSL is simply App() */
require('uWebSockets.js').SSLApp({
  /* There are more SSL options, cut for brevity */
  key_file_name: 'misc/key.pem',
  cert_file_name: 'misc/cert.pem',
}).ws('/*', {
  /* There are many common helper features */
  idleTimeout: 30,
  maxBackpressure: 1024,
  maxPayloadLength: 512,
  compression: DEDICATED_COMPRESSOR_3KB,
  /* For brevity we skip the other events (upgrade, open, ping, pong, close) */
  message: (ws, message, isBinary) => {
    /* You can do app.publish('sensors/home/temperature', '22C') kind of pub/sub as well */
    /* Here we echo the message back, using compression if available */
    let ok = ws.send(message, isBinary, true);
  }
}).get('/*', (res, req) => {
  /* It does Http as well */
  res.writeStatus('200 OK').writeHeader('IsExample', 'Yes').end('Hello there!');
}).listen(9001, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 9001');
  }
});

优势

用直接针对 Linux 内核的原生代码编写,使得它比任何 JavaScript 实现都要快。

实战证明

运行 Bitfinex.com 的交易 API,每天处理约 1.7 亿美元的交易量。同时还运行着 Trello,为他们的 5000 万用户提供实时板块更新服务。

轻松安装

用 npm install uNetworking/uWebSockets.js#v18.8.0 或其他类似版本。不需要编译器。

  • 可在 Linux、macOS 和 Windows(ARM64、x64)上运行。Node.js 10、11、12、13、14 和 15。
  • 从这个 GitHub 仓库安装,而不是 NPM 注册表;点击 "fork" 来获得你自己的副本

商业支持

uNetworking AB 是一家瑞典的咨询和承包公司,处理与 µWebSockets 有关的任何事情:开发、支持和客户成功。

如果您正在建设大型项目,需要咨询或有其他业务咨询,请不要犹豫,给我们发邮件。我们会找出对双方都有利的方法,并确保您不会踏入许多常见的陷阱。

特别感谢 BitMEX、Bitfinex、Google、Coinbase、Bitwyre 和 deepstreamHub 自 2016 年以来让项目本身在 GitHub 上茁壮成长 -- 没有这些华丽的公司,这个项目是不可能的。

同一家族

µWebSockets.js 是 Node.js 对 µWebSockets 的集成;是独立的C++项目。如果对性能非常重视,您不一定要使用 JavaScript/Node.js,而是可以直接使用 µWebSockets 用 C++ 编写应用程序。它的工作方式完全一样,将为那些高要求的应用提供无与伦比的性能。无论哪种方式--这两个项目都遵循相同的安全测试、合规性测试,并获得相同的错误修复和功能。它们是同一个家族的一部分。

🤝 许可使用

知识产权,保留所有权利。

在有明确声明的情况下,源代码采用 Apache License 2.0 许可,这是一个允许的 OSI 批准的许可,限制很少。修改后的 "fork" 应该包含许可的源代码,并以其他产品名称提供。如果您对此有任何不确定,请在假设之前询问。

 

(The first version translated by vz on 2020.12.13)

(Revised by vz on 2023.9.17)

 

Overview

Name With OwneruNetworking/uWebSockets.js
Primary LanguageC++
Program languageMakefile (Language Count: 4)
PlatformLinux, Mac, Windows
License:Apache License 2.0
Release Count98
Last Release Namev20.43.0 (Posted on )
First Release Namev0.0.1 (Posted on )
Created At2018-07-27 23:18:30
Pushed At2024-04-30 09:47:45
Last Commit At2024-04-30 11:15:26
Stargazers Count7.4k
Watchers Count119
Fork Count552
Commits Count589
Has Issues Enabled
Issues Count333
Issue Open Count12
Pull Requests Count56
Pull Requests Open Count1
Pull Requests Close Count43
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

:zap: Simple performance

µWebSockets.js is a web server bypass for Node.js that reimplements eventing, networking, encryption, web protocols, routing and pub/sub in highly optimized C++. As such, µWebSockets.js delivers web serving for Node.js, 8.5x that of Fastify and at least 10x that of Socket.IO. It is also the built-in web server of Bun.

  • We recommend, for simplicity installing with yarn add uWebSockets.js@uNetworking/uWebSockets.js#v20.19.0 or any such release.

  • Browse the documentation and see the main repo. There are tons of examples but here's the gist of it all:

/* Non-SSL is simply App() */
require('uWebSockets.js').SSLApp({

  /* There are more SSL options, cut for brevity */
  key_file_name: 'misc/key.pem',
  cert_file_name: 'misc/cert.pem',
  
}).ws('/*', {

  /* There are many common helper features */
  idleTimeout: 32,
  maxBackpressure: 1024,
  maxPayloadLength: 512,
  compression: DEDICATED_COMPRESSOR_3KB,

  /* For brevity we skip the other events (upgrade, open, ping, pong, close) */
  message: (ws, message, isBinary) => {
    /* You can do app.publish('sensors/home/temperature', '22C') kind of pub/sub as well */
    
    /* Here we echo the message back, using compression if available */
    let ok = ws.send(message, isBinary, true);
  }
  
}).get('/*', (res, req) => {

  /* It does Http as well */
  res.writeStatus('200 OK').writeHeader('IsExample', 'Yes').end('Hello there!');
  
}).listen(9001, (listenSocket) => {

  if (listenSocket) {
    console.log('Listening to port 9001');
  }
  
});

:handshake: Permissively licensed

Intellectual property, all rights reserved.

Where such explicit notice is given, source code is licensed Apache License 2.0 which is a permissive OSI-approved license with very few limitations. Modified "forks" should be of nothing but licensed source code, and be made available under another product name. If you're uncertain about any of this, please ask before assuming.

To the top