Git Product home page Git Product logo

mongoose-socket.io's Introduction

mongoose-socket.io

Build Status npm npm

Mongoose plugin to automatically emit needed events via Socket.io.

Build live applications in few minutes. You can thank me later ;) ...

This modules lets you attach socket.io directly to your models and emit event at any mongoose hook you want in an extremly flexible way.

Installation

npm install --save mongoose-socket.io

Usage

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const mongooseSocketIo = require('mongoose-socket.io');

let UserSchema = new Schema({
  name: String,
  email: String,
  something: Array
});

UserSchema.plugin(mongoooseSocketIo, {
  io, // Socketio instance
  prefix: 'user',
  namespace: function(doc){
    return ['test1','test2']
  },
  room: ['room1', 'room2'],
  events: {
    create: {
      select: 'email skills',
      populate: {
        path: 'skills',
        select: 'name'
      },
      map: function(data) {
        //Do some last mapping/modification
        data.provider = data.email.split('@').pop();
        return data;
      }
    },
    update: {
      populate: 'skills'
    },
    remove: false
  },
  partials: [
    {
      eventName: 'custom_event',
      triggers: 'name',
      select: 'name email',
      populate: 'something' //if it is a reference...
    }
  ],
  debug: false
})

Options

io

This is the result of the socket.io initialization

prefix

Prefixes all event names that are being emitted from this model. (Example for creation event: 'PREFIX:create') You can pass the following types:

Type Guide
String Pass a string to emit to a single & fixed namespace
Function Pass a function that will be resolved right before emitting. Accepts string as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as eventname

namespace

Define what namespace you want to emit the event on. This option is very powerful You can pass the following types:

Type Guide
String Pass a string to emit to a single & fixed namespace
Array Pass an array to emit to every namespace contained in the array. The array accepts strings and functions
Function Pass a function that will be resolved right before emitting. Accepts string or array as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as namespace

room

Define what room you want to emit the event on. This option is very powerful You can pass the following types:

Type Guide
String Pass a string to emit to a single & fixed room
Array Pass an array to emit to every room contained in the array. The array accepts strings and functions
Function Pass a function that will be resolved right before emitting. Accepts string or array as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as room

events

Define what events you want to emit. The following events are available:

  • create
  • update
  • remove

Enabling / Disabling events

events: {
  create: false || true
}

Advanced configuration

This configuration uses the same syntax as mongoose. Actually it uses mongoose functionallity to make it possible. So you can include exclude single & multiple fields

events: {
  create: {
    select: 'name email',
    populate: 'something'
  }
}
events: {
  create: {
    select: 'name email',
    populate: {
      path: 'something',
      select: 'example'
    },
    map: function(data) {
      //Do some last mapping/modification
      data.provider = data.email.split('@').pop();
      return data;
    }
  }
}

Each event does also support a map function which gives you the ability to modify your data one last time before sending. This could be used to merge/delete/add values.

partials

Partials gives you the power to create custom events with custom triggers and custom data. Very flexible... The settings do also use mongoose built in functionality. So be sure to checkout mongoose documentation. You have the map function here as well.

partials: [
  {
    eventName: 'custom_eventname',
    triggers: 'name email',
    select: 'email',
    populate: 'something'
  }
]
partials: [
  {
    eventName: 'custom_eventname',
    triggers: 'something',
    select: 'something name',
    populate: {
      path: 'something',
      select: 'random'
    }
  }
]

debug

Set this to true if you want to output info about all emitted events.

Development & Testing

npm run dev starts a server at localhost:3000.

License

The MIT License Copyright (c) Carsten Jacobsen

mongoose-socket.io's People

Contributors

crsten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mongoose-socket.io's Issues

Dynamic namespace initialization

In order to get namespacing to work properly, we need to find a way of initializing namespaces.
Otherwise socket.io returns "Invalid namespace" until the first event has passed (and created the namespace)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.