{"id":17826418,"url":"https://github.com/crsten/mongoose-socket.io","last_synced_at":"2025-03-18T23:31:00.934Z","repository":{"id":57302262,"uuid":"89385627","full_name":"crsten/mongoose-socket.io","owner":"crsten","description":null,"archived":false,"fork":false,"pushed_at":"2017-06-19T17:24:54.000Z","size":13,"stargazers_count":13,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T20:06:01.984Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crsten.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-25T16:59:14.000Z","updated_at":"2024-02-15T15:42:33.000Z","dependencies_parsed_at":"2022-09-14T22:50:39.463Z","dependency_job_id":null,"html_url":"https://github.com/crsten/mongoose-socket.io","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-socket.io","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-socket.io/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-socket.io/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-socket.io/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crsten","download_url":"https://codeload.github.com/crsten/mongoose-socket.io/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244325261,"owners_count":20435074,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-27T18:47:41.981Z","updated_at":"2025-03-18T23:31:00.665Z","avatar_url":"https://github.com/crsten.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-socket.io\n[![Build Status](https://travis-ci.org/crsten/mongoose-socket.io.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/crsten/mongoose-socket.io)\n[![npm](https://img.shields.io/npm/dt/mongoose-socket.io.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-socket.io)\n[![npm](https://img.shields.io/npm/v/mongoose-socket.io.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-socket.io)\n\n[Mongoose](http://mongoosejs.com/) plugin to automatically emit needed events via [Socket.io](https://socket.io/).\n\nBuild live applications in few minutes. You can thank me later ;) ...\n\nThis modules lets you attach socket.io directly to your models and emit event at any mongoose hook you want in an extremly flexible way.\n\n## Installation\n\n`npm install --save mongoose-socket.io`\n\n## Usage\n\n```js\nconst mongoose = require('mongoose');\nconst Schema = mongoose.Schema;\nconst mongooseSocketIo = require('mongoose-socket.io');\n\nlet UserSchema = new Schema({\n  name: String,\n  email: String,\n  something: Array\n});\n\nUserSchema.plugin(mongoooseSocketIo, {\n  io, // Socketio instance\n  prefix: 'user',\n  namespace: function(doc){\n    return ['test1','test2']\n  },\n  room: ['room1', 'room2'],\n  events: {\n    create: {\n      select: 'email skills',\n      populate: {\n        path: 'skills',\n        select: 'name'\n      },\n      map: function(data) {\n        //Do some last mapping/modification\n        data.provider = data.email.split('@').pop();\n        return data;\n      }\n    },\n    update: {\n      populate: 'skills'\n    },\n    remove: false\n  },\n  partials: [\n    {\n      eventName: 'custom_event',\n      triggers: 'name',\n      select: 'name email',\n      populate: 'something' //if it is a reference...\n    }\n  ],\n  debug: false\n})\n```\n\n## Options\n\n### io\n\nThis is the result of the socket.io initialization\n\n### prefix\n\nPrefixes all event names that are being emitted from this model. *(Example for creation event: 'PREFIX:create')*\nYou can pass the following types:\n\n| Type | Guide |\n| ---- | ----- |\n| String | Pass a string to emit to a single \u0026 fixed namespace |\n| 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 |\n\n### namespace\n\nDefine what namespace you want to emit the event on. This option is **very powerful**\nYou can pass the following types:\n\n| Type | Guide |\n| ---- | ----- |\n| String | Pass a string to emit to a single \u0026 fixed namespace |\n| Array | Pass an array to emit to every namespace contained in the array.  **The array accepts strings and functions** |\n| 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 |\n\n### room\n\nDefine what room you want to emit the event on. This option is **very powerful**\nYou can pass the following types:\n\n| Type | Guide |\n| ---- | ----- |\n| String | Pass a string to emit to a single \u0026 fixed room |\n| Array | Pass an array to emit to every room contained in the array.  **The array accepts strings and functions** |\n| 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 |\n\n### events\n\nDefine what events you want to emit. The following events are available:\n\n- create\n- update\n- remove\n\n#### Enabling / Disabling events\n\n```js\nevents: {\n  create: false || true\n}\n```\n\n#### Advanced configuration\n\nThis configuration uses the same syntax as mongoose. Actually it uses mongoose functionallity to make it possible. So you can include exclude single \u0026 multiple fields\n\n```js\nevents: {\n  create: {\n    select: 'name email',\n    populate: 'something'\n  }\n}\n```\n\n```js\nevents: {\n  create: {\n    select: 'name email',\n    populate: {\n      path: 'something',\n      select: 'example'\n    },\n    map: function(data) {\n      //Do some last mapping/modification\n      data.provider = data.email.split('@').pop();\n      return data;\n    }\n  }\n}\n```\n\nEach event does also support a map function which gives you the ability to modify your data one last time before sending.\nThis could be used to merge/delete/add values.\n\n### partials\n\nPartials gives you the power to create custom events with custom triggers and custom data. Very flexible...\nThe settings do also use mongoose built in functionality. So be sure to checkout mongoose documentation.\nYou have the `map` function here as well.\n\n```js\npartials: [\n  {\n    eventName: 'custom_eventname',\n    triggers: 'name email',\n    select: 'email',\n    populate: 'something'\n  }\n]\n```\n\n```js\npartials: [\n  {\n    eventName: 'custom_eventname',\n    triggers: 'something',\n    select: 'something name',\n    populate: {\n      path: 'something',\n      select: 'random'\n    }\n  }\n]\n```\n\n### debug\n\nSet this to true if you want to output info about all emitted events.\n\n## Development \u0026 Testing\n\n`npm run dev` starts a server at [localhost:3000](http://localhost:3000).\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\nCopyright (c) Carsten Jacobsen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-socket.io","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrsten%2Fmongoose-socket.io","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-socket.io/lists"}