# socket-mit

> ``` npm i --save socket-mit ```

Latest version **1.1.2** (published 2018-10-02) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2018-10-02 |
| First published | 2018-10-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 25.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ymblender |

## Links

- npm: https://www.npmjs.com/package/socket-mit
- Repository: https://github.com/ymzuiku/socket-mit
- Homepage: https://github.com/ymzuiku/socket-mit#readme
- Issues: https://github.com/ymzuiku/socket-mit/issues
- npm.io page: https://npm.io/package/socket-mit

## Dependencies (5)

- [ws](https://npm.io/package/ws.md) ^6.0.0
- [uws](https://npm.io/package/uws.md) 10.148.1
- [pino](https://npm.io/package/pino.md) ^5.6.2
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.0
- [pino-pretty](https://npm.io/package/pino-pretty.md) ^2.2.1

## Recent versions

- 1.1.2 (latest) — 2018-10-02
- 1.1.0 — 2018-10-02
- 1.0.9 — 2018-10-02
- 1.0.8 — 2018-10-02
- 1.0.7 — 2018-10-02
- 1.0.6 — 2018-10-02
- 1.0.5 — 2018-10-02
- 1.0.4 — 2018-10-02
- 1.0.3 — 2018-10-02
- 1.0.2 — 2018-10-02
- 1.0.1 — 2018-10-02
- 1.0.0 — 2018-10-02

## README

# Use socket like http

```
npm i --save socket-mit
```

## example

### server:

index.js

```js
const mit = require('socket-mit');
const path = require('path');

// auto load routers/xxx.js and router/xxx/index.js;
// ignore underline begins files, like: _xxx.js
mit.load(path.resolve(__dirname, 'routers'));
mit.listen(4000);
```

routers/hello.js

```js
module.exports = function(data, ws) {
  console.log(data);
  ws.dispatch({ name: 'server:hello-client', age: 100 });
};
```

### client:

```js
const mit = require('socket-mit/client');
var ws = mit('ws://127.0.0.1:4000');
ws.onopen = function() {
  ws.dispatch(
    'hello',
    {
      name: 'client: hello-server',
      age: 200,
    },
    res => {
      console.log('catch-server-message:', res);
    },
  );
};
```

## auto some routers

server

```js
const mit = require('socket-mit');
const path = require('path');
mit.load(path.resolve(__dirname, 'routers'));
mit.listen(4000);
```

```js
// routers/login.js
module.exports = {
  useEmail: function(data, ws, opts) {
    // ..
  },
  usePhone: function(data, ws, opts) {
    // ..
  },
  sendEmail: {
    changePassword: function(data, ws, opts) {
      // ..
    },
    bindEmail: {
      bindNow: function(data, ws, opts) {
        // ..
      },
      bindNextDay: function(data, ws, opts) {
        // ..
      },
    },
  },
};
```

client

```js
var ws = mit('ws://127.0.0.1:4000');
ws.onopen = function() {
  // load server routers/login.js function
  ws.dispatch('login.useEmail');
  ws.dispatch('login.sendEmail.changePassword');
  ws.dispatch('login.sendEmail.bindEmail.bindNow');
};
```

## if use mysql or other database

server:

```js
const mit = require('../serve');
const Sequelize = require('sequelize');
const { resolve } = require('path');

const sequelize = new Sequelize('test', 'root', '111Asd', { dialect: 'mysql' });
sequelize.authenticate();

// send { db: sequelize } to opts
mit.use({ db: sequelize });
mit.load(resolve(__dirname, 'routers'));
mit.listen(4000);
```

routers/hello.js

```js
module.exports = function(data, ws, opts) {
  console.log(opts); // { db: sequelize }
  // use opts.db do someting
};
```

## use local log files

set `logPath`, log save in local file, use [pino](https://github.com/pinojs/pino)

```js
const mit = require('../serve');
const path = require('path');

mit.use({ logPath: path.resolve(__dirname, 'logs') });
mit.autoLoad(path.resolve(__dirname, 'routers'));
mit.listen(4000);
```

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