# gopush

> this is a solution for mutiple websocket base applications and easy to scale, gopush provide a agent layer which take care all the websocket and http requests from clients, all you only need to do is deploy your websocket base app behide this agent.

Latest version **0.1.1** (published 2018-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install gopush
pnpm add gopush
yarn add gopush
bun add gopush
```

Provides the commands `gopush`, `gopush-create`.

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-06-29 |
| First published | 2017-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 20 |
| Unpacked size | 1.1 MB |
| Known vulnerabilities | 0 (+8 in 5 direct dependencies) |
| Install scripts | no |
| Author | hcnode@gmail.com |
| Maintainers | hcnode |

## Links

- npm: https://www.npmjs.com/package/gopush
- Repository: https://github.com/hcnode/gopush
- Homepage: https://github.com/hcnode/gopush#readme
- Issues: https://github.com/hcnode/gopush/issues
- npm.io page: https://npm.io/package/gopush

## Dependencies (20)

- [pm2](https://npm.io/package/pm2.md) ^2.4.0
- [debug](https://npm.io/package/debug.md) ~2.6.0
- [jquery](https://npm.io/package/jquery.md) ^3.1.1
- [morgan](https://npm.io/package/morgan.md) ^1.7.0
- [express](https://npm.io/package/express.md) ^4.16.3
- [ioredis](https://npm.io/package/ioredis.md) ^2.5.0
- [request](https://npm.io/package/request.md) ^2.79.0
- [ts-node](https://npm.io/package/ts-node.md) ^7.0.0
- [upgrade](https://npm.io/package/upgrade.md) ^1.1.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^2.1.2
- [bootstrap](https://npm.io/package/bootstrap.md) ^3.3.7
- [socket.io](https://npm.io/package/socket.io.md) ^1.7.2
- [typescript](https://npm.io/package/typescript.md) ^2.8.3
- [body-parser](https://npm.io/package/body-parser.md) ~1.16.0
- [cookie-parser](https://npm.io/package/cookie-parser.md) ~1.4.3
- [serve-favicon](https://npm.io/package/serve-favicon.md) ~2.3.2
- [faye-websocket](https://npm.io/package/faye-websocket.md) ^0.11.1
- [socket.io-redis](https://npm.io/package/socket.io-redis.md) ^3.0.0
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^2.1.1
- [file-stream-rotator](https://npm.io/package/file-stream-rotator.md) ^0.1.0

## Recent versions

- 0.1.1 (latest) — 2018-06-29
- 0.1.0 — 2018-06-29
- 0.0.9 — 2018-05-14
- 0.0.8 — 2017-05-10
- 0.0.7 — 2017-05-05
- 0.0.6 — 2017-05-05
- 0.0.5 — 2017-04-28
- 0.0.4 — 2017-04-28
- 0.0.3 — 2017-04-28
- 0.0.2 — 2017-04-27
- 0.0.1 — 2017-04-27

## README

## What is this ?
this is a solution for mutiple websocket base applications and easy to scale, gopush provide a agent layer which take care all the websocket and http requests from clients, all you only need to do is deploy your websocket base app behide this agent.

## What is the problem ?
when using socket.io with cluster mode, you probably meet the sticky sesson issue.

![without_agent](https://raw.githubusercontent.com/hcnode/gopush/master/charts/issue_with_cluster_socket.io.png)

## Some of solutions
* one of the solutions to keep session persistence is using [upstream block to support load balance and session sticky](https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/)

```
upstream socket_nodes {
    ip_hash;
    server srv1.app.com:5000 weight=5;
    server srv2.app.com:5000;
    server srv3.app.com:5000;
    server srv4.app.com:5000;
}
```
in some complex situation ip_hash sometimes is not as precise as we expect to keep the session sticky, in addition, you have to keep maintaining upstream block while some nodes need to scale or down.

* socket.io wrapped with [socket-io-sticky-session](https://github.com/wzrdtales/socket-io-sticky-session) to keep the session sticky.

how this libary work is firstly launch a master as an agent and then fork a number of slaves as backend servers, all requests go to the agent first, then get the client ip by header of x-forwarded-for, then pipe the request to a specify worker by calculating hash of the ip, which is a way to force slave worker stick to a conrresponding ip. the main problem of this approach is only one master, and you may probably can not scale it(I am not sure, is it?), and you may can not deploy this application by using pm2 with cluster mode neither.

## How this solution work ?
gopush provide agent server, which act as a route server, all requests including http and websocket go to agent server, then agent server pipe these requests to conrresponding service with "unique product and uid" identity and by implementing them in few hooks config, and gopush also offer a kind of built-in and out of box and pm2 support websocket service which you can develop some business base on "room" concept.

![with_agent](https://raw.githubusercontent.com/hcnode/gopush/master/charts/modules.png)

## Usages
(depend on pm2 and redis, mongodb is optional)

1. `npm i gopush -g` 

2. `mkdir project-name` 

3. `cd project-name`
 
4. `gopush-create` to create your own config and hooks

5. **edit config:**

    include local, develop and production config json file in config folder, which one would be used depend on the environment of your server:

    * local.json : mac or windows
    * production : process.env.NODE_ENV === 'production'
    * develop : neither above

    ```javsacript
    {
        "agentPort" : 6003, // port of agent server
        "serverPortStart" : 7050, // port of socket.io server
        "senecaPort" : 60000, // port of microservice server
        "serverIps" : ["127.0.0.1"], // server ips
        "serverWorkers" : 2, // instances of socket.io server
        "agentWorkers" : 1, // instances of agent server
        "redisAdapter" : { // redis server
            "host" : "127.0.0.1",
            "port": 6379
        },
        "mongodb" : "mongodb://localhost/push_service"
    }
    ```

6. **edit hooks:**

    include agent middleware `onupgrade-middleware.js` and `agent-middleware.js`, admin middleare `admin-middleware.js`, each of them also have a production copy(*_production.js), which applied in production environment.

    * agent-middleware.js

    ```javascript
    module.exports = function(app){
        app.use(function(req, res, next){
            res.locals.uid = req.cookies.uid; // define res.locals.uid is necessary or response 430 error
            next();
        });
    }
    ```

    * onupgrade-middleware.js

    ```javascript
    module.exports = function(req){ 
        // return Promise object and resolve uid
        return new Promise((resolve, reject) => {
            var cookies = parseCookies(req);
            resolve(cookies.uid);
        });
    }
    ```

    * admin-middleware.js

    ```javascript
    module.exports = function(app){
        app.use((req, res, next) => {
            // do something to verify the user
            next();
        })
    }
    ```

7. run `gopush` after finishing config 

8. visit `http://localhost:6003` you will see `hello world` if every thing is ok.

9. visit `http://localhost:6012/pushManager/admin/` to subscribe message or manage message

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