8.2.10 • Published 6 years ago

sugo-hub v8.2.10

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
6 years ago

Build Status npm Version JS Standard

Hub server of SUGOS

SUGO-Hub works as a hub to connect SUGO-Actors and SUGO-Callers.

Table of Contents

Requirements

Installation

$ npm install sugo-hub --save

Usage

#!/usr/bin/env node

/**
 * This is an example to setup hub server
 */

'use strict'

const sugoHub = require('sugo-hub')

async function tryExample () {
  // Start sugo-hub server
  let hub = sugoHub({
    // Options here
  })

  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}

tryExample().catch((err) => console.error(err))

By default, SUGOS-Cloud provides WebSocket interfaces with following URLs:

URLDescription
/actorsWebSocket namespace for SUGO-Actors
/callersWebSocket namespace for SUGO-Callers
/observersWebSocket namespace for SUGO-Observers

For more detail, see API Guide

Advanced Usage

SUGO cloud also provide bunch of options for building more complex applications.

Using Redis Server

By default, SUGO-Hub save state to json files. This may cause performance slow down on production. It is recommended to setup redis server for storage.

#!/usr/bin/env node

/**
 * This is an example to setup hub server with redis
 */

'use strict'

const sugoHub = require('sugo-hub')

async function tryRedisExample () {
  let hub = sugoHub({
    // Using redis server as storage
    storage: {
      // Redis setup options (see https://github.com/NodeRedis/node_redis)
      redis: {
        host: '127.0.0.1',
        port: '6379',
        db: 1
      }
    },
    endpoints: { /* ... */ },
    middlewares: [ /* ... */ ],
    static: [ /* ... */ ]
  })

  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}
tryRedisExample().catch((err) => console.error(err))

Define HTTP Endpoints

SUGO-Hub uses Koa as http framework. You can define custom koa handlers on endpoints field.

#!/usr/bin/env node

/**
 * This is an example to setup hub server with endpoints
 */

'use strict'

const sugoHub = require('sugo-hub')

async function tryExample () {
  let hub = sugoHub({
    storage: { /* ... */ },
    // HTTP route handler with koa
    endpoints: {
      '/api/user/:id': {
        'GET': (ctx) => {
          let { id } = ctx.params
          /* ... */
          ctx.body = { /* ... */ }
        }
      }
    },
    middlewares: [ /* ... */ ],
    static: [ /* ... */ ]
  })

  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}
tryExample().catch((err) => console.error(err))

Define HTTP Middlewares

For cross-endpoint handling, add koa middleware function to middlewares field.

Note that static middlewares are provided as build-in middleware and you can serve static files by just setting directory names to static field.

#!/usr/bin/env node

/**
 * This is an example to setup hub server with middlewares
 */

'use strict'

const sugoHub = require('sugo-hub')

async function tryMiddleareExample () {
  let hub = sugoHub({
    storage: { /* ... */ },
    // HTTP route handler with koa
    endpoints: { /* ... */ },
    // Custom koa middlewares
    middlewares: [
      async function customMiddleware (ctx, next) {
        /* ... */
        await next()
      }
    ],
    // Directory names to server static files
    static: [
      'public'
    ]
  })

  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}
tryMiddleareExample().catch((err) => console.error(err))

Use Authentication

By providing authenticate filed, you can authenticate sockets connecting.

#!/usr/bin/env node

/**
 * This is an example to setup hub server with aut
 */

'use strict'

const sugoHub = require('sugo-hub')

async function tryAuthExample () {
  let hub = sugoHub({
    storage: { /* ... */ },
    endpoints: { /* ... */ },
    /**
     * Auth function
     * @param {Object} socket - A socket connecting
     * @param {Object} data - Socket auth data
     * @returns {Promise.<boolean>} - OK or not
     */
    authenticate (socket, data) {
      let tokenStates = { /* ... */ }
      let ok = !!tokenStates[ data.token ]
      return Promise.resolve(ok)
    },
    middlewares: [ /* ... */ ],
    static: [ /* ... */ ]
  })

  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}

tryAuthExample().catch((err) => console.error(err))

Register Local Actors

If you want to use actors on the same environment with hub, pass actors instances to localActors option of hub.

#!/usr/bin/env node

/**
 * This is an example to setup hub server with local actors
 */

'use strict'

const sugoHub = require('sugo-hub')
const sugoActor = require('sugo-actor')

async function tryLocalExample () {
  let hub = sugoHub({
    storage: { /* ... */ },
    endpoints: { /* ... */ },
    middlewares: [ /* ... */ ],
    static: [ /* ... */ ],

    /**
     * Local actors for the hub
     * @type {Object<string, SugoActor>}
     */
    localActors: {
      'my-actor-01': sugoActor({
        modules: {
          say: {
            sayYes: () => 'Yes from actor01'
          }
        }
      })
    }
  })

  // Local actors automatically connect to the hub when it start listening
  await hub.listen(3000)

  console.log(`SUGO Cloud started at port: ${hub.port}`)
}

tryLocalExample().catch((err) => console.error(err))

License

This software is released under the Apache-2.0 License.

Links

8.2.10

6 years ago

8.2.9

6 years ago

8.2.8

6 years ago

8.2.7

6 years ago

8.2.6

6 years ago

8.2.5

6 years ago

8.2.4

6 years ago

8.2.3

7 years ago

8.2.2

7 years ago

8.2.1

7 years ago

8.1.3

7 years ago

8.1.2

7 years ago

8.1.1

7 years ago

8.0.6

7 years ago

8.0.5

7 years ago

8.0.4

7 years ago

8.0.3

7 years ago

8.0.2

7 years ago

8.0.1

7 years ago

7.0.2

7 years ago

7.0.1

7 years ago

6.0.8

7 years ago

6.0.7

7 years ago

6.0.6

7 years ago

6.0.5

7 years ago

6.0.4

7 years ago

6.0.3

7 years ago

6.0.2

7 years ago

6.0.1

7 years ago

5.4.3

7 years ago

5.4.2

7 years ago

5.4.1

7 years ago

5.3.3

7 years ago

5.3.2

7 years ago

5.3.1

7 years ago

5.2.4

7 years ago

5.2.3

7 years ago

5.2.2

7 years ago

5.2.0

7 years ago