Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chef-js/socket
Command Line Interface Static Files Server written in TypeScript for Single Page Applications serving in Node with Socket.IO
https://github.com/chef-js/socket
cli express http-server javascript nodejs nodejs-server socket-io typescript websockets
Last synced: about 1 month ago
JSON representation
Command Line Interface Static Files Server written in TypeScript for Single Page Applications serving in Node with Socket.IO
- Host: GitHub
- URL: https://github.com/chef-js/socket
- Owner: chef-js
- Created: 2022-02-10T17:03:04.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-11T20:25:42.000Z (over 1 year ago)
- Last Synced: 2024-05-10T15:03:19.486Z (9 months ago)
- Topics: cli, express, http-server, javascript, nodejs, nodejs-server, socket-io, typescript, websockets
- Language: TypeScript
- Homepage: https://chef-js-socket.herokuapp.com/
- Size: 325 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# chef-socket
**web-sockets** micro-service manager and **static files server** at the same port,
designed for **node** written in **typescript**, with **tests**
- `express` for routing (and `socket.io` for websockets)
## Command-Line Running
```bash
$ npx chef-socket folder [--debug] [--ssl] [--port 443] [--plugin path/to/plugin.js]
```## Installation
```bash
$ yarn add chef-socket
```## Minimal Chat Demo
https://chef-js-socket.herokuapp.com/
```bash
$ yarn add chef-socket
$ yarn chef-socket node_modules/chef-socket/demo --plugin node_modules/chef-core/chat.js
```Minimal configuration is specifying folder, then it serves it from http://localhost:4200
```ts
const startServer = require("chef-socket");
const config = { folder: "docs" };startServer(config).then((server: Express.Application) => {
// server api is get, post, any
server.any("/*", (req: Express.Request, res: Express.Response) => {
res.end("200 OK");
});
});
```## Configuration
For more information about config parameters read:
- The default configuration https://github.com/chef-js/core#configuration
- The parameters types https://chef-js.github.io/core/types/Config.html
## Plugins
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.
### STEP 1: Before Connection
- client -> `socket.io-client` connects to `location.origin.replace(/^http/, 'ws')`
- server -> waits for any incoming `config.join` events### STEP 2: Connection
- client -> sends `join` event with room name (topic/plugin name)
- server -> if such plugin is configured joins client to that plugin### STEP 3: After Connection
- client -> does some actions (emits, receives)
- server -> plugin responds to websocket actions### STEP 4: Finish Connection
- client -> disconnects after some time
- server -> broadcasts to all plugins from room that client left (`config.leave`)## API
- 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 the `server` instance.
- plugins receive (and send) the data in the format of:```ts
{
id, // WebSocket id - this is automatically added
event, // event name as string
data, // any data accompanying the event
}
```## License
MIT