Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 18, 2018
2 parents d1aa8cc + 834119b commit 98c3f27
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 46 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,26 @@
<a name="1.0.7"></a>
## [1.0.7](https://github.com/adonisjs/adonis-websocket/compare/v1.0.6...v1.0.7) (2018-03-18)



<a name="1.0.6"></a>
## 1.0.6 (2018-03-16)


### Features

* **channel:** add option to define channel controller ([fa792d6](https://github.com/adonisjs/adonis-websocket/commit/fa792d6))
* **cluster:** add cluster pub/sub file to deliver messages ([6a4c52a](https://github.com/adonisjs/adonis-websocket/commit/6a4c52a))
* **middleware:** add support for Adonis style middleware ([f0eaf3d](https://github.com/adonisjs/adonis-websocket/commit/f0eaf3d))



<a name="1.0.5"></a>
## 1.0.5 (2018-03-16)


### Features

* rewrite ([a8b7d15](https://github.com/adonisjs/adonis-websocket/commit/a8b7d15))
* **config:** add sample config file ([4bf58c5](https://github.com/adonisjs/adonis-websocket/commit/4bf58c5))
* **encoder:** use json encoder by default ([01aa591](https://github.com/adonisjs/adonis-websocket/commit/01aa591))
53 changes: 53 additions & 0 deletions clusterPubSub.js
@@ -0,0 +1,53 @@
'use strict'

/*
* adonis-websocket
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const cluster = require('cluster')
const debug = require('debug')('adonis:websocket')

/**
* Calls a callback by looping over cluster workers
*
* @method worketIterator
*
* @param {Function} callback
*
* @return {void}
*/
function workerIterator (callback) {
Object.keys(cluster.workers).forEach((index) => callback(cluster.workers[index]))
}

/**
* Delivers the message to all the cluster workers, apart from the
* one that sends the message
*
* @method deliverMessage
*
* @param {String} message
*
* @return {void}
*/
function deliverMessage (message) {
workerIterator((worker) => {
if (this.process.pid === worker.process.pid) {
return
}
debug('delivering message to %s', worker.process.pid)
worker.send(message)
})
}

module.exports = function () {
if (!cluster.isMaster) {
throw new Error('clusterPubSub can be only be used with cluster master')
}
workerIterator((worker) => worker.on('message', deliverMessage))
}
15 changes: 1 addition & 14 deletions config/index.js
Expand Up @@ -62,18 +62,5 @@ module.exports = {
| been elapsed, the client will consider server as dead.
|
*/
clientAttempts: 3,

/*
|--------------------------------------------------------------------------
| Allow No Channels
|--------------------------------------------------------------------------
|
| The Websocket server will fail to start if no channels have been registered.
| It is recommended to keep the flag `false`, since there is no point in
| accepting new connections without any channels/subscriptions to respond
| to.
|
*/
allowNoChannels: false
clientAttempts: 3
}
20 changes: 20 additions & 0 deletions config/socket.js
@@ -0,0 +1,20 @@
'use strict'

/*
|--------------------------------------------------------------------------
| Websocket
|--------------------------------------------------------------------------
|
| This file is used to register websocket channels and start the Ws server.
| Learn more about same in the official documentation.
| https://adonisjs.com/docs/websocket
|
| For middleware, do check `wsKernel.js` file.
|
*/

const Ws = use('Ws')

Ws.channel('chat', ({ socket }) => {
console.log('user joined with %s socket id', socket.id)
})
39 changes: 39 additions & 0 deletions config/wsKernel.js
@@ -0,0 +1,39 @@
'use strict'

const Ws = use('Ws')

/*
|--------------------------------------------------------------------------
| Global middleware
|--------------------------------------------------------------------------
|
| Global middleware are executed on each Websocket channel subscription.
|
*/
const globalMiddleware = [
]


/*
|--------------------------------------------------------------------------
| Named middleware
|--------------------------------------------------------------------------
|
| Named middleware are defined as key/value pairs. Later you can use the
| keys to run selected middleware on a given channel.
|
| // define
| {
| auth: 'Adonis/Middleware/Auth'
| }
|
| // use
| Ws.channel('chat', 'ChatController').middleware(['auth'])
*/
const namedMiddleware = {
}


Ws
.registerGlobal(globalMiddleware)
.registerNamed(namedMiddleware)
42 changes: 42 additions & 0 deletions instructions.js
@@ -0,0 +1,42 @@
'use strict'

/**
* adonis-websocket
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const path = require('path')

async function copySocketFile (cli) {
const inFile = path.join(__dirname, 'config', 'socket.js')
const outFile = path.join(cli.helpers.appRoot(), 'start/socket.js')
await cli.copy(inFile, outFile)
cli.command.completed('create', 'start/socket.js')
}

async function copyKernelFile (cli) {
const inFile = path.join(__dirname, 'config', 'wsKernel.js')
const outFile = path.join(cli.helpers.appRoot(), 'start/wsKernel.js')
await cli.copy(inFile, outFile)
cli.command.completed('create', 'start/wsKernel.js')
}

async function copyConfigFile (cli) {
const inFile = path.join(__dirname, 'config', 'index.js')
const outFile = path.join(cli.helpers.configPath(), 'socket.js')
await cli.copy(inFile, outFile)
cli.command.completed('create', 'config/socket.js')
}

module.exports = async (cli) => {
try {
await copySocketFile(cli)
await copyConfigFile(cli)
await copyKernelFile(cli)
} catch (error) {
// ignore error
}
}
37 changes: 37 additions & 0 deletions instructions.md
@@ -0,0 +1,37 @@
# Register provider

The provider must be registered inside `start/app.js` file.

```js
const providers = [
'@adonisjs/websocket/providers/WsProvider'
]
```

## Channels

The next step is to open `start/socket.js` and register websocket channels.

```js
const Ws = use('Ws')

Ws.channel('chat', ({ socket }) => {
console.log('new socket joined %s', socket.id)
})
```

## Middleware

The middleware for websocket are kept in the `start/wsKernel.js` file.


```js
const Ws = use('Ws')

const globalMiddleware = []
const namedMiddleware = {}

Ws
.registerGlobal(globalMiddleware)
.registerNamed(namedMiddleware)
```
37 changes: 24 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/websocket",
"version": "1.0.5",
"version": "1.0.7",
"description": "websocket server for Adonis framework",
"main": "index.js",
"scripts": {
Expand All @@ -20,20 +20,20 @@
"license": "MIT",
"devDependencies": {
"@adonisjs/fold": "^4.0.7",
"@adonisjs/framework": "^5.0.4",
"@adonisjs/framework": "^5.0.5",
"@adonisjs/sink": "^1.0.16",
"coveralls": "^3.0.0",
"cz-conventional-changelog": "^2.1.0",
"japa": "^1.0.6",
"japa-cli": "^1.0.1",
"nyc": "^11.6.0",
"standard": "^11.0.0",
"standard": "^11.0.1",
"test-console": "^1.1.0"
},
"dependencies": {
"@adonisjs/generic-exceptions": "^2.0.0",
"@adonisjs/websocket-packet": "^1.0.5",
"co-compose": "^4.0.0",
"@adonisjs/middleware-base": "^1.0.0",
"@adonisjs/websocket-packet": "^1.0.6",
"cuid": "^2.1.0",
"debug": "^3.1.0",
"emittery": "^0.3.0",
Expand All @@ -45,9 +45,14 @@
"src/Context/index.js",
"src/ClusterHop/index.js",
"src/ClusterHop/sender.js",
"test"
"clusterPubSub.js",
"test",
"config"
]
},
"standard": {
"ignore": ["config"]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
Expand Down

0 comments on commit 98c3f27

Please sign in to comment.