DEV Community

Cover image for Verb :: Fast Everything Server
Wess Cope
Wess Cope

Posted on

Verb :: Fast Everything Server

A fast, modern server framework for Bun with multi-protocol support. Build HTTP, HTTP/2, WebSocket, gRPC, UDP, and TCP servers with the same intuitive API.

https://github.com/wess/verb


Verb is designed specifically for the Bun run time, for more information on Bun checkout https://bun.sh


Features

  • Multi-Protocol Support - HTTP, HTTP/2, WebSocket, gRPC, UDP, TCP
  • Unified API - Same interface across all protocols
  • Runtime Protocol Switching - Switch between protocols dynamically
  • Built for Bun - Native Bun APIs for maximum performance
  • TypeScript First - Full type safety out of the box

Install / Add

# Install dependencies
bun add github:wess/verb

Enter fullscreen mode Exit fullscreen mode

Basic Usage:

HTTP Server

import { createServer } from "verb";

const app = createServer();

app.get("/", (req, res) => {
  res.json({ message: "Hello World!" });
});

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Protocol Selection

import { createServer, ServerProtocol } from "verb";

// HTTP/2 server
const http2Server = createServer(ServerProtocol.HTTP2);

// WebSocket server
const wsServer = createServer(ServerProtocol.WEBSOCKET);
wsServer.websocket({
  open: (ws) => ws.send("Connected!"),
  message: (ws, message) => ws.send(`Echo: ${message}`)
});

// gRPC server
const grpcServer = createServer(ServerProtocol.GRPC);
grpcServer.addMethod("UserService", {
  name: "GetUser",
  handler: async (request) => ({ id: request.id, name: "John" })
});

// UDP server
const udpServer = createServer(ServerProtocol.UDP);
udpServer.onMessage((message) => {
  console.log("UDP:", message.data.toString());
});

// TCP server
const tcpServer = createServer(ServerProtocol.TCP);
tcpServer.onConnection((connection) => {
  connection.write("Welcome to TCP server!");
});
Enter fullscreen mode Exit fullscreen mode

Fluent API

import { server } from "verb";

const httpApp = server.http();
const grpcApp = server.grpc();
const udpApp = server.udp();
Enter fullscreen mode Exit fullscreen mode

Protocol Gateway

Switch between protocols at runtime with the same routes:

import { ProtocolGateway, ServerProtocol } from "verb";

const gateway = new ProtocolGateway();

// Define routes that work across HTTP-based protocols
gateway.defineRoutes((app) => {
  app.get("/api/status", (req, res) => {
    res.json({ 
      status: "healthy",
      protocol: gateway.getCurrentProtocol() 
    });
  });
});

// Start with HTTP
gateway.listen(3000);

// Switch to HTTP/2
gateway.switchProtocol(ServerProtocol.HTTP2);
gateway.listen(3001);

// Switch to WebSocket
const wsServer = gateway.switchProtocol(ServerProtocol.WEBSOCKET);
wsServer.websocket({
  open: (ws) => ws.send("WebSocket ready!")
});
gateway.listen(3002);
Enter fullscreen mode Exit fullscreen mode

Available Protocols

Protocol Enum Description
HTTP/1.1 ServerProtocol.HTTP Standard HTTP server
HTTP/2 ServerProtocol.HTTP2 HTTP/2 with multiplexing
WebSocket ServerProtocol.WEBSOCKET WebSocket with HTTP routes
gRPC ServerProtocol.GRPC gRPC service definitions
UDP ServerProtocol.UDP UDP message handling
TCP ServerProtocol.TCP TCP connection management

API Reference

HTTP-based Servers (HTTP, HTTP/2, WebSocket)

// Route methods
app.get(path, handler)
app.post(path, handler)
app.put(path, handler)
app.delete(path, handler)
app.patch(path, handler)

// Middleware
app.use(middleware)

// Start server
app.listen(port, hostname?)
Enter fullscreen mode Exit fullscreen mode

WebSocket Server

app.websocket({
  open: (ws) => { /* connection opened */ },
  message: (ws, message) => { /* message received */ },
  close: (ws) => { /* connection closed */ }
});
Enter fullscreen mode Exit fullscreen mode

gRPC Server

app.addService(service)
app.addMethod(serviceName, method)
Enter fullscreen mode Exit fullscreen mode

UDP Server

app.onMessage((message) => { /* handle message */ })
app.send(data, port, address)
Enter fullscreen mode Exit fullscreen mode

TCP Server

app.onConnection((connection) => { /* handle connection */ })
app.onData((connection, data) => { /* handle data */ })
Enter fullscreen mode Exit fullscreen mode

Development

# Run tests
bun test

# Lint code
bun run lint

# Format code
bun run format
Enter fullscreen mode Exit fullscreen mode

Top comments (0)