Skip to content

Commit

Permalink
Merge pull request #1 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] Sparks
  • Loading branch information
scott-wyatt authored May 8, 2019
2 parents 2c0e7a6 + 83688e8 commit b991c8c
Show file tree
Hide file tree
Showing 23 changed files with 7,851 additions and 538 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,27 @@ Create the config file: `config/realtime.ts `

```js
export const realtime = {
prefix: null, // The prefix to use for the primus endpoint
primus:{
options:{
//these options are passed directly to the Primus constructor: https://github.com/primus/primus#getting-started
}
}
},
plugins: {
// Plugins are a key and a library passed to primus.use
redis: require('primus-redis-rooms')
}
}
```

## Client
You can set the config path for the generated primus.js file by either setting

`config.main.paths.www` (common defaults for webserver spools) or through `config.realtime.path` which should likely be to a static directory.

You can include the primus client library as a script:
```
<script src="/primus/primus.js"></script>
<script src="/<realtime.path>/primus.js"></script>
```

## License
Expand Down
5 changes: 4 additions & 1 deletion archetype/config/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const realtime = {
path: null,
prefix: null,
primus: {
options: {}
}
},
plugins: {}
}
52 changes: 47 additions & 5 deletions lib/RealtimeSpool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Spool } from '@fabrix/fabrix/dist/common'
import { Utils as RouterUtils } from '@fabrix/spool-router'
import * as Primus from 'primus'

const primusDefaults = {
Expand All @@ -7,6 +8,7 @@ const primusDefaults = {

import * as Validator from './validator'

import * as api from './api/index'
import * as config from './config/index'
import * as pkg from '../package.json'

Expand All @@ -17,7 +19,7 @@ export class RealtimeSpool extends Spool {
super(app, {
config: config,
pkg: pkg,
api: {}
api: api
})

this.extensions = {
Expand Down Expand Up @@ -61,19 +63,38 @@ export class RealtimeSpool extends Spool {
}

async initialize() {
const primusConfig = this.app.config.get('realtime.primus')

const isExpress = this.app.config.get('web.server') === 'express'
const listener = isExpress ? 'webserver:http' : 'webserver:http:ready'
const pathname = this.app.config.get('realtime.prefix') ? `${this.app.config.get('realtime.prefix')}/primus` : 'primus'

// The path for primus/primus.js
const path = this.app.config.get('realtime.path') || this.app.config.get('main.paths.www') || __dirname

const primusConfig = Object.assign(
{},
{ pathname: pathname },
this.app.config.get('realtime.primus')
)

const plugins = this.app.config.get('realtime.plugins') || {}

return new Promise((resolve, reject) => {
this.app.once('webserver:http:ready', (httpServer) => {
this.app.once(listener, (httpServer) => {

if (Array.isArray(httpServer)) {
httpServer = httpServer[0]
}

try {
this._sockets = new Primus(httpServer, Object.assign(primusDefaults, primusConfig.options))
this._sockets = Primus(httpServer, Object.assign(
{},
primusConfig.options,
primusDefaults
))
}
catch (err) {
this.app.log.error('Primus failed to create', err)
reject(err)
}

Expand All @@ -86,7 +107,22 @@ export class RealtimeSpool extends Spool {
reject(err)
}

return resolve()
// Attach spark connection events
Object.keys(this.app.sparks || {}).forEach(k => {
this.sockets.on('connection', this.app.sparks[k].connection)
})

// Attach spark disconnection events
Object.keys(this.app.sparks || {}).forEach(k => {
this.sockets.on('disconnection', this.app.sparks[k].disconnection)
})

this._sockets.save(path + '/primus.js', function save(err) {
if (err) {
return reject(err)
}
return resolve()
})
})
})
}
Expand All @@ -110,6 +146,12 @@ export class RealtimeSpool extends Spool {
}
return
})
.then( () => {
if (!this._sockets.Spark) {
throw new Error('Socket Spark does not exist')
}
return
})
.catch(err => {
return Promise.reject(err)
})
Expand Down
14 changes: 14 additions & 0 deletions lib/Spark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {FabrixGeneric as Generic, FabrixModel} from '@fabrix/fabrix/dist/common'

/**
* @module Spark
* @description Spark
*/
export class Spark extends Generic {
connection(spark) {
throw new Error('Connection should be overwritten by sub class!')
}
disconnection(spark) {
throw new Error('Disconnection should be overwritten by sub class!')
}
}
15 changes: 15 additions & 0 deletions lib/api/controllers/PrimisController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FabrixController as Controller } from '@fabrix/fabrix/dist/common'

/**
* @module CartController
* @description Cart Controller.
*/
// TODO lock down certain requests by Owner(s)
export class PrimusController extends Controller {
spec(req, res) {
return res.send(this.app.sockets.spec)
}
library(req, res) {
return res.send(this.app.sockets.library())
}
}
1 change: 1 addition & 0 deletions lib/api/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PrimusController } from './PrimisController'
6 changes: 6 additions & 0 deletions lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as controllers from './controllers'
import * as sparks from './sparks'
export {
// controllers
sparks
}
1 change: 1 addition & 0 deletions lib/api/sparks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
1 change: 1 addition & 0 deletions lib/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { spool } from './spool'
export { realtime } from './realtime'
export { routes } from './routes'
6 changes: 5 additions & 1 deletion lib/config/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const realtime = {
path: null,
prefix: null,
primus: {
transformer: 'engine.io',
options: {}
}
},
plugins: {}
}
26 changes: 26 additions & 0 deletions lib/config/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const routes = {
// '/primus/spec': {
// 'GET': {
// handler: 'PrimusController.spec',
// config: {
// prefix: 'realtime.prefix'
// }
// }
// },
// '/primus/library': {
// 'GET': {
// handler: 'PrimusController.library',
// config: {
// prefix: 'realtime.prefix'
// }
// }
// },
// '/primus/primus.js': {
// 'GET': {
// file: 'primus.js',
// config: {
// prefix: 'realtime.prefix'
// }
// }
// },
}
16 changes: 8 additions & 8 deletions lib/config/spool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Trailpack Configuration
* Spool Configuration
*
* @see {@link http://fabrixjs.io/doc/trailpack/config
* @see {@link https://fabrix.app/docs/spools/config
*/
export const spool = {

/**
* API and config resources provided by this Trailpack.
* API and config resources provided by this Spool.
*/
provides: {
resources: [],
Expand All @@ -18,24 +18,24 @@ export const spool = {
},

/**
* Configure the lifecycle of this pack; that is, how it boots up, and which
* order it loads relative to other trailpacks.
* Configure the lifecycle of this spool; that is, how it boots up, and which
* order it loads relative to other spools.
*/
lifecycle: {
configure: {
/**
* List of events that must be fired before the configure lifecycle
* method is invoked on this Trailpack
* method is invoked on this Spool
*/
listen: [ ],
listen: [ 'spool:router:configured'],

/**
* List of events emitted by the configure lifecycle method
*/
emit: [ ]
},
initialize: {
listen: [ ],
listen: [ 'spool:router:initialized'],
emit: [ ]
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { RealtimeSpool } from './RealtimeSpool'
export { Spark } from './Spark'
2 changes: 2 additions & 0 deletions lib/schemas/realtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import * as joi from 'joi'

export const realtimeConfig = joi.object().keys({
path: joi.string().allow(null, ''),
prefix: joi.string().allow(null, ''),
primus: joi.object(),
plugins: joi.object()
})
Loading

0 comments on commit b991c8c

Please sign in to comment.