uwebsocket-plus
v0.0.3
Published
A powerful, plugin-based WebSocket library built on top of Elysia with TypeScript support. UWebSocket+ provides advanced room management, real-time messaging, and extensible plugin architecture.
Maintainers
Readme
🚀 UWebSocket+
A powerful, plugin-based WebSocket library built on TypeScript and Elysia.
✨ Features
- 🏠 Room Management - Multi-room chat support
- 👥 User Management - Real-time user presence
- 🔌 Plugin System - Extensible architecture
- ⚡ High Performance - Built with Bun and Elysia
- 🔒 Type Safety - Full TypeScript support
📦 Installation
bun add uwebsocket-plus
# or
npm install uwebsocket-plus🎯 Quick Start
Simple WebSocket Server
import { Websocket } from "uwebsocket-plus";
const ws = Websocket({});
ws.create("/", (route) => {
route.on("open", (client) => {
console.log("Client connected:", client.connectionId);
});
route.on("message", (client, message) => {
route.broadcast(`${client.connectionId}: ${message}`);
});
});
ws.listen(3000);Elysia Integration
import { Elysia } from "elysia";
import { Websocket } from "uwebsocket-plus";
const app = new Elysia();
const ws = Websocket(app);
ws.create("/chat", (route) => {
route.on("message", (client, message) => {
route.broadcast(message);
});
});
app.listen(3000);Room System
ws.create("/:roomId", (route) => {
route.on("open", (client) => {
const roomId = client.data.params.roomId;
client.joinRoom(roomId);
ws.broadcastToRoom(roomId, {
type: "join",
user: client.connectionId
});
});
route.on("message", (client, message) => {
const roomId = client.data.params.roomId;
ws.broadcastToRoom(roomId, {
type: "message",
user: client.connectionId,
data: message
});
});
});🔌 Plugin System
import { Plugin, Websocket } from "uwebsocket-plus";
const myPlugin = new Plugin()
.setName("my-plugin")
.withStore(() => [["counter", 0]])
.withMethods((ws) => ({
increment: () => ws.elysia.store.counter++
}))
.onMessage((client, message) => {
console.log("Message:", message);
})
.build();
const ws = Websocket({
plugins: [myPlugin] as const
});
// Use plugin method
ws.increment();📚 API
WebSocket Methods
ws.create(path, callback)- Create WebSocket endpointws.broadcast(message)- Send message to all clientsws.broadcastToRoom(roomId, message)- Send message to roomws.getClients(options)- Get client listws.listen(port)- Start server
Client Methods
client.joinRoom(roomId)- Join roomclient.leaveRoom(roomId)- Leave roomclient.send(message)- Send message to clientclient.data.store- Client data storeclient.connectionId- Unique connection ID
Plugin Methods
setName(name)- Set plugin namewithStore(factory)- Define data storewithMethods(factory)- Add custom methodsonOpen/onMessage/onClose(handler)- Event handlers
🎮 Frontend
const ws = new WebSocket('ws://localhost:3000/room1');
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Message:', data);
};
ws.send(JSON.stringify({
type: 'chat',
message: 'Hello!'
}));📄 License
MIT License - LICENSE
