This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
581 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.