0.0.1 • Published 3 years ago
yows v0.0.1
yows
File Based Events WebSocket Server
Warning This project is under development
Installation
pnpm add yows
npm add yows
yarn add yowsUsage
Create a folder called
eventseither in the root of your project or in asrcfolder.In
eventsfolder create a file calledeventName.tsstructured as this:
import type { WebSocket } from "ws";
type Data = {
t: "eventName";
text: string;
};
export default function eventName(ws: WebSocket, data: Data) {
if (data.text === "hello") {
ws.send(JSON.stringify({ t: "eventName", text: "world" }));
}
}- In root of your project run
yows
Optionally add start script to your package.json
{
"scripts": {
"start": "yows"
}
}# Locally from dependencies
pnpm start
# If you installed `yows` globally
yows- Connect to
ws://localhost:3000and send
{
"t": "eventName",
"text": "hello"
}- You should receive
{
"t": "eventName",
"text": "world"
}Default events
To use one of the default events, you can create a file prepended with _ in events folder with a name of the event you want to use.
Currently usable default events are:
- _connect
- _disconnect
- _error
- _message
Example of a connection event:
import type { WebSocket } from "ws";
import { randomUUID } from "node:crypto";
type ExtendedWebSocket = WebSocket & {
id: string;
};
export default function connect(ws: ExtendedWebSocket) {
ws.id = randomUUID();
ws.send(JSON.stringify({ t: "welcome", id: ws.id }));
}CLI Options
- -p, --port port Port which
yowsshould listen on
0.0.1
3 years ago