# socket-zenjs

> A Express/WS Nodejs framework

Latest version **1.2.3** (published 2020-09-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install socket-zenjs
pnpm add socket-zenjs
yarn add socket-zenjs
bun add socket-zenjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.3 |
| Published | 2020-09-22 |
| First published | 2020-09-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 27.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | carterscadden |

## Links

- npm: https://www.npmjs.com/package/socket-zenjs
- npm.io page: https://npm.io/package/socket-zenjs

## Dependencies (2)

- [ws](https://npm.io/package/ws.md) ^7.3.1
- [child_process](https://npm.io/package/child_process.md) ^1.0.2

## Recent versions

- 1.2.3 (latest) — 2020-09-22
- 1.2.2 — 2020-09-22
- 1.2.1 — 2020-09-22
- 1.2.0 — 2020-09-22
- 1.1.10 — 2020-09-22
- 1.1.9 — 2020-09-21
- 1.1.8 — 2020-09-20
- 1.1.7 — 2020-09-20
- 1.1.6 — 2020-09-20
- 1.1.5 — 2020-09-17
- 1.1.4 — 2020-09-17
- 1.1.3 — 2020-09-16
- 1.1.2 — 2020-09-16
- 1.1.1 — 2020-09-16
- 1.1.0 — 2020-09-16
- … 11 more at https://npm.io/package/socket-zenjs/versions

## README

# socket-zen

A Nodejs + ws framework for websockets.

Just connect to the server under the ws://exampleurl:
and send data from the front end like:

```js
const ws = new Websocket("ws://exampleurl");

ws.onopen = () => {
  ws.send(
    JSON.stringify({
      url: "/ping",
      data: "", // any valid json,
      // any other attributes can be added
    })
  );
};
```

## abilities

- useable with any nodejs web library or framework
- before middleware
- routers
- global state / injections
- onConnection handleing
- url catching
- query handleing
- full typescript / typing support

## examples

### Example With base "http" package.

```ts
import http from "http";
import Zenjs from "socket-zenjs";

const server = http.createServer((req, res) => {}).listen(8080);

const socketServer = new Zenjs()
  .onConnection((connection) => {
    console.info("connection made! " + connection.uuid);
  })
  .on("/ping", (connection, req) => {
    connection.send(200, "pong");
  })
  .catch((connection, req) => {
    connection.send(404, `Url of "${req.url}" not found`);
  })
  .initSocket(server);
```

### Example with the "express" package

```ts
import express from "express";
import Zenjs from "socket-zenjs";
import apiRouter from "./routers/apiRouter";

const app = express();

const server = app.listen(8080);

const socketServer = new Zenjs()
  // a use will be run for every socket message recieved by the server
  .use((connection, req, stop) => {
    if (req.authentication !== "") {
      connection.send(400, "Missing authentication for authenticated route");
      connection.close();
      stop();
    }
  })
  // will be used to check for a ?help query paramater
  .use('', (connection, req, stop) => {
    if (req.query.help !== undefined) {
      connection.send(200, `
        Routes:
          *: requires a 'authentication' field on the message object
            ex:
              {authentication
                url: string:
                data: any;
                authentication: string;
              }

          /api:
            /users: returns a list of users
      `);
    }
  })
  .addRouter(apiRouter);
  .initSocket(server);

... // In "./routers/apiRouter"
import Zenjs from "socket-zenjs";
const users = ['user1', 'user2'];

const apiRouter = ZenJs.Router('/api'); // Routes take one paramater, the base url

apiRouter
  .use((connection, req) => console.info('api router hit'));
  .on('/users', (connection, req) => connection.send(200, users))
  //.useRouter(any other router, which will use the base '/api' that this router has as well)
```

### injections / state

```js
const server = new Zenjs({ name: "Bob" });

server.on("", (connection, request, { name }) => {
  // use name here :)
});

const apiRouter = server
  .Router("/api")
  .on("/name", (connection, request, { name }) => {
    connection.send(200, name);
  });

server.addRouter(apiRouter);
```

---
_Source: https://npm.io/package/socket-zenjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
