web-sockets micro-service manager and static files server at the same port,
designed for node written in typescript, with tests
express
for serving files (andsocket.io
for websockets)
https://chef-js-socket.herokuapp.com/
$ yarn add chef-socket
$ yarn chef-socket node_modules/chef-socket/demo --plugin node_modules/chef-core/chat.js
$ [PORT=4200] yarn chef-socket folder [--debug] [--ssl] [--key example.key] [--cert example.crt] [--plugin path/to/file.js]
const startServer = require("chef-socket");
startServer({
// this enables http/ws logs
debug: process.argv.includes("--debug"),
// port on which the server listens
port: Number(process.env.PORT || 4200),
// you can use --plugin ./path/to/plugin.js any number of times
plugins: {},
// handshake event
join: "/join",
// disconnect from room event
leave: "/leave",
// folder to static serve files
folder: process.argv[2],
// ssl = undefined | { key, cert }
ssl: process.argv.includes("--ssl") ? ssl : undefined,
}).then((server) => {
// server api is get, post, any
server.any("/*", (res, req) => {
res.end("200 OK");
});
});
PORT=4200
- choose server portfolder
- folder you want to serve static files from--debug
- show logs--ssl
- start as https server, with self signed certificate--key example.key
- path to real certificate key, use with--ssl
--cert example.crt
- path to real certificate, use with--ssl
--plugin path/to/file.js
- path toWSPlugin
, can use multiple times
$ yarn add chef-socket
The plugins are a mighty thing, think of them like chat rooms,
after a client handshakes the chat room, his messages start being forwarded to that room,
and it is being handled there by the room's own plugin.
This means you can have for example: a chat server and other unrelated websocket services
at the same port as the files server too. One client may be in many rooms.
- client ->
socket.io-client
connects tolocation.origin.replace(/^http/, 'ws')
- server -> waits for any incoming
config.join
events
- client -> sends
join
event with room name (topic/plugin name) - server -> if such plugin is configured joins client to that plugin
- client -> does some actions (emits, receives)
- server -> plugin responds to websocket actions
- client -> disconnects after some time
- server -> broadcasts to all plugins from room that client left (
config.leave
)
- a plugin is a function
(ws, { id, event, data })
that is called each time the frontend websocket emits to server - context (
this
) of each plugin is theserver
instance. - plugins receive (and send) the data in the format of:
{
id, // WebSocket id - this is automatically added
event, // event name as string
data, // any data accompanying the event
}
MIT