Channels, pipelines and codecs for TCP, UDP and WebSocket, built for game servers.
Why Netify
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
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.
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 })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 })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 })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 })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 })Built for performance
A Bun process runs on one core. These spread a server over the rest.
ClusterRuns the same server in several processes that share one port, with graceful stops and rolling reloads.7.1M msg/s TCP echo on 4 coresShardingShardLinkGives each process its own zones and port, links them directly, and moves players between them.3M messages a second between two shardsWorker threadsWorkerGroupRuns a server on several threads in one process, with shared memory between them.OffloadingOffloadHandlerSends slow work from the pipeline to a pool of threads and passes the result on.3.7x the throughput on 4 threadsEach message takes a short path, and these keep it short in your code too.
ByteBufPoolDecoders reuse frame objects, so steady traffic makes almost no garbage.Bit packingBitWriterWrites fields of 1 to 32 bits, with Quantizer for floats in a known range.Flush batchingFlushConsolidationHandlerMerges the flushes made during one read into a single write.Rate limitsGlobalTrafficShapingHandlerHolds each player, or the whole server, to a byte rate and slows senders down instead of dropping them.MetricsMetricsHandlerCounts connections, bytes, messages and handler time, so you add shards when the numbers say so.BroadcastsChannelGroupEncodes a message once and sends the same bytes to every player.Netify and Netty
If you know Netty, you already know how Netify fits together. These are the bits that work differently on Bun.
Coming from NettyBenchmarks
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
The guide starts with a TCP echo server and builds up from there.