1.2.3 • Published 4 years ago

socket-zenjs v1.2.3

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

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:

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.

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

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

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);
1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.0.91

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago