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

resm-broadcaster

v0.1.0

Published

A nodejs server to client and client to server communication.

Downloads

8

Readme

Broadcaster

A nodejs broadcasting library that focus with server to client and client to server communication.

Prerequisite

Installation

npm install --save resm-broadcaster

Current Driver Supported

  • SocketIO
  • Custom Driver is supported. You can easily extend drivers to be used.
  • AdonisJS Framework Supported.

Usage

Define a Config.

Config.js

'use strict'

const ENV = new (require('resm-env'))({
    path: 'env file path'
});

module.exports = {
    log: true,
    // Default connection
    default: ENV.get('BROADCAST_CONNECTION', 'socketio'),
    /**
     * List of connections that can be used
     */
    connections: {
        socketio: {
            driver: 'socketio',
            client: {
                uri: ENV.get('SOCKET_URL')
            },
            /**
             * {optional} Specify the port to be use,
             * If you have bootstrap a server instance
             * this is not requierd
             */
            port: ENV.get('SOCKET_PORT', '9000'),
            options: {},
            // io middleware
            middlewares: [],
        }
    }
};

Bootstrap Services

Initialize first the BroadcastManager and BroadcastEvent Service.

Default

Server.js

const config = require('Config');

const BroadcastManager = require('resm-broadcaster/BroadcastManager');

Broadcaster = new BroadcastManager(config);

const BroadcastEvent = new (require('resm-broadcaster/BroadcastEvent'))(Broadcaster);

// boot listeners
/**
 * Listener Path Directory/Files
 */
const listeners = [
        __dirname + '/app/Listener'
    ],

BroadcastEvent.bootListeners(listeners);

// add hook to be called when socket establish connection
BroadcastEvent.addHook((socket) => {
    // other hooks here
    socket.joinRoom('test')
});
AdonisJS Framework

Define a provider to boot the setup same as below.

BroadcastServiceProvider.js

const { ServiceProvider } = require('@adonisjs/fold')

class BroadcastServiceProvider extends ServiceProvider {
    register () {
        const Config = use('Config');
        let broadcastConfig = Config.get('broadcaster');

        this.app.singleton('Broadcaster', () => {
            const BroadcastManager = require('resm-broadcaster/BroadcastManager');

            const Server = use('Server');

            return new BroadcastManager(
                broadcastConfig
            );
        });

        this.app.singleton('BroadcastEvent', () => {
            const BroadcastEvent = require('resm-broadcaster/BroadcastEvent');
            const BroadcastManager = use('Broadcaster');

            return new BroadcastEvent(
                BroadcastManager,
                broadcastConfig
            );
        });
    }

    boot() {
        const BroadcastEvent = use('BroadcastEvent');
        const appRoot = use('Helpers').appRoot();

        const listeners = [
            appRoot+'/app/Listeners'
        ];

        BroadcastEvent.bootListeners(listeners);

        // add hook to be called in socket.
        BroadcastEvent.addHook((socket) => {
            socket.joinRoom('sample');
        });
    }
}

module.exports = BroadcastServiceProvider

Firing Events.

After successful bootstrap BroadCastEvent. Create a Event file that implements the BroadcastEventContract. In ES6 there is no implements yet so we will utilize the extends.

Define Event Class

Sample.js

const BroadcastEvent = require('resm-broadcaster/Contracts/BroadcastEventContract');

class Sample extends BroadcastEvent {

    constructor(data) {
        super();
        console.log('constructing event data', data);

        this.sampleData = data;
    }

    /**
     * Data to be send when the event was emitted.
     * @return object
     */
    data() {
        // input logic here

        return this.sampleData;
    }

    /**
     * Event Channel to be broadcasted.
     * @return array
     */
    broadcastOn() {
        // this will emit in channel test
        return 'test'; // Default: null
    }

    /**
     * Event Name to be broadcast
     * @return string
     */
    broadcastAs() {
        return 'sample-event-emit';
    }
}
Default Implementation

Server.js

const Sample = require('Sample');

const data = {
    name: 'Rej Mediodia',
    message: 'This is a test'
};

BroadCastEvent.fireEvent(new Sample(data));
AdonisJS Implementation

In any class/controller or even in the Listener SampleController

'use strict'
const BroadcastEvent = use('BroadcastEvent');
const CreatedEventClass = use('App/Events/Sample');
class GeneralController {

    save() {
        //save logic here
        let data = {
            // data to be submitted
        };
        BroadcastEvent.fire(new CreatedEventClass(data));
    }
}

module.exports = GeneralController;
Client Side

All joined in channel test will received this in Client.js.

socket.on('sample-event-emit', function(data) {
    console.log(data);
    /**
     * data = {
     *   name: 'Rej Mediodia',
     *   message: 'This is a test'
     */
};
});

Listener

Client.js

const data = {
    name: 'Rej Mediodia',
    message: 'This is from client'
};

socket.emit('sample-event', data);

We need to define first our Listener File that implements the BroadcastListenContract. In ES6 there is no implements yet so we will utilize the extends.

SampleListener.js

const BroadcastListenContract = require('resm-broadcaster/Contracts/BroadcastListenContract')

class SampleListener extends BroadcastListenContract {

    handle(data, socket) {
        console.log('received data', data);
        /**
         * data = {
         *   name: 'Rej Mediodia',
         *   message: 'This is from client'
         * }
    }

    eventName() {
        return 'sample-event';
    }
}

module.exports = SampleListener;

Todo

  • Security for SocketIODriver
  • Test Cases

Inspiration

License

MIT