Skip to content
Get started
alpha · fast already, not done yet

Netty's design
at Bun's speed

Channels, pipelines and codecs for TCP, UDP and WebSocket, built for game servers.

Get started

Why Netify

Less code between
the socket and your game

Most network code is the same in every server. Split the byte stream into messages, encode the replies, keep track of connections, deal with clients that read too slowly. Netify gives you those parts, and your code only sees whole messages.

Handlers are plain classes, and the same one runs over TCP, UDP or WebSocket. Moving a game from desktop to the browser doesn't mean rewriting the server. If you've used Netty, the names will be familiar.

Protocols

One pipeline for
every protocol

Handlers don't know which protocol they run on. Write the game logic once, then serve TCP to native clients, WebSocket to browsers and reliable UDP where latency matters.

  1. Read the TCP guide
ts
import {
  
InboundHandlerAdapter
,
LineBasedFrameDecoder
,
ServerBootstrap
,
StringDecoder
,
type
Channel
,
type
HandlerContext
,
} from '@netifyjs/netify' class
Chat
extends
InboundHandlerAdapter
<string> {
public override
channelRead
(
context
:
HandlerContext
,
line
: string): void {
context
.
writeAndFlushSync
(`${
line
}\n`)
} } await new
ServerBootstrap
()
.
childHandler
((
channel
:
Channel
): void => {
channel
.
pipeline
.
addLast
<
LineBasedFrameDecoder
>(
'lines', new
LineBasedFrameDecoder
({
maxLength
: 512 }),
) .
addLast
<
StringDecoder
>('text', new
StringDecoder
())
.
addLast
<
Chat
>('chat', new
Chat
())
}) .
bind
({
port
: 7000 })
ts
import {
  
DatagramBootstrap
,
DatagramPacket
,
InboundHandlerAdapter
,
type
DatagramChannel
,
type
HandlerContext
,
} from '@netifyjs/netify' class
Ping
extends
InboundHandlerAdapter
<
DatagramPacket
> {
public override
channelRead
(
context
:
HandlerContext
,
packet
:
DatagramPacket
,
): void {
context
.
writeSync
(
packet
.
reply
('pong'))
} public override
channelReadComplete
(
context
:
HandlerContext
):
Promise
<void> {
return
context
.
flush
()
} } await new
DatagramBootstrap
()
.
handler
((
channel
:
DatagramChannel
): void => {
channel
.
pipeline
.
addLast
<
Ping
>('ping', new
Ping
())
}) .
bind
({
port
: 9000 })
ts
import {
  
InboundHandlerAdapter
,
ReliableBootstrap
,
ReliableMessage
,
type
DatagramChannel
,
type
HandlerContext
,
} from '@netifyjs/netify' class
Game
extends
InboundHandlerAdapter
<
ReliableMessage
> {
public override
channelRead
(
context
:
HandlerContext
,
message
:
ReliableMessage
,
): void { if (
message
.
lane
=== 0)
message
.
reply
('got it')
} } await new
ReliableBootstrap
()
.
options
({
lanes
: [
{
delivery
: 'reliable-ordered' },
{
delivery
: 'unreliable-sequenced' },
], }) .
handler
((
channel
:
DatagramChannel
): void => {
channel
.
pipeline
.
addLast
<
Game
>('game', new
Game
())
}) .
bind
({
port
: 7777 })
ts
import {
  
InboundHandlerAdapter
,
TextWebSocketFrame
,
WebSocketBootstrap
,
type
HandlerContext
,
type
WebSocketChannel
,
} from '@netifyjs/netify' class
Lobby
extends
InboundHandlerAdapter
<
TextWebSocketFrame
> {
public override
channelActive
(
context
:
HandlerContext
): void {
const
channel
=
context
.
channel
as
WebSocketChannel
channel
.
subscribe
('lobby')
} public override
channelRead
(
context
:
HandlerContext
,
frame
:
TextWebSocketFrame
,
): void { const
channel
=
context
.
channel
as
WebSocketChannel
channel
.
publish
('lobby',
frame
.
text
())
} } await new
WebSocketBootstrap
()
.
options
({
path
: '/play' })
.
childHandler
((
channel
:
WebSocketChannel
): void => {
channel
.
pipeline
.
addLast
<
Lobby
>('lobby', new
Lobby
())
}) .
bind
({
port
: 8080 })
ts
import {
  
FullHttpRequest
,
FullHttpResponse
,
HttpObjectAggregator
,
HttpResponseStatus
,
HttpServerCodec
,
InboundHandlerAdapter
,
ServerBootstrap
,
type
Channel
,
type
HandlerContext
,
} from '@netifyjs/netify' class
Hello
extends
InboundHandlerAdapter
<
FullHttpRequest
> {
public override
channelRead
(
context
:
HandlerContext
,
request
:
FullHttpRequest
,
): void { const
response
= new
FullHttpResponse
(
HttpResponseStatus
.
OK
, 'hello')
response
.
setContentLength
(
response
.
content
.
readableBytes
)
response
.
setKeepAlive
(
request
.
isKeepAlive
())
context
.
writeAndFlushSync
(
response
)
} } await new
ServerBootstrap
()
.
childHandler
((
channel
:
Channel
): void => {
channel
.
pipeline
.
addLast
<
HttpServerCodec
>('http', new
HttpServerCodec
())
.
addLast
<
HttpObjectAggregator
>(
'body', new
HttpObjectAggregator
(65536),
) .
addLast
<
Hello
>('hello', new
Hello
())
}) .
bind
({
port
: 8000 })

Netify and Netty

Same model,
different runtime

If you know Netty, you already know how Netify fits together. These are the bits that work differently on Bun.

Coming from Netty
NettyJava · JVM
NetifyTypeScript · Bun
01 Building blocks
Channels, pipelines, handlers and codecs
The same, and most class names carry over
02 Threads
An EventLoopGroup with a thread per core
One thread per process, with Cluster or WorkerGroup for more cores
03 Slow handlers
Blocking one stalls every channel on its event loop
Handlers can be async. That connection waits and the rest carry on
04 Writes
A ChannelFuture, or voidPromise to skip it
A Promise, or writeSync to skip it
05 Buffers
Reference counted, and you release them
Garbage collected. Only pooled frames you keep need retain
06 WebSocket
A codec in the pipeline
Bun's native server with topic publish, or a codec in the pipeline
07 Reliable UDP
QUIC through the incubator codec
ReliableBootstrap, with acks, resends and ordering
08 Scaling out
Up to you
Cluster for processes, ShardLink and ShardMap for shards

Benchmarks

23xfaster than NestJS over TCP
11xthe broadcasts of Colyseus
  1. NetifyBun2,515,718 msg/s
  2. gRPCBun134,404
  3. ZeroMQNode133,136
  4. MoleculerBun131,174
  5. NestJSBun107,792

Length framed echo, 64 clients with 32 messages in flight. Median of three runs, every server on Bun except those marked Node. How it's measured

Write your first server

The guide starts with a TCP echo server and builds up from there.

Released under the MIT licence.