npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sugo-hub

v8.2.10

Published

Hub server of SUGOS

Downloads

36

Readme

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:

| URL | Description | | --- | ----------- | | /actors | WebSocket namespace for [SUGO-Actors][sugo_actor_url] | | /callers | WebSocket namespace for [SUGO-Callers][sugo_caller_url] | | /observers | WebSocket namespace for [SUGO-Observers][sugo_observer_url] |

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