• This repository has been archived on 06/Nov/2019
  • Stars
    star
    310
  • Rank 134,900 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pub/sub for Node.js and MongoDB

mubsub

Mubsub is a pub/sub implementation for Node.js and MongoDB. It utilizes Mongo's capped collections and tailable cursors to notify subscribers of inserted documents that match a given query.

NPM Build Status

Example

var mubsub = require('mubsub');

var client = mubsub('mongodb://localhost:27017/mubsub_example');
var channel = client.channel('test');

client.on('error', console.error);
channel.on('error', console.error);

channel.subscribe('bar', function (message) {
    console.log(message.foo); // => 'bar'
});

channel.subscribe('baz', function (message) {
    console.log(message); // => 'baz'
});

channel.publish('bar', { foo: 'bar' });
channel.publish('baz', 'baz');

Usage

Create a client

You can pass a Db instance or a URI string. For more information about the URI format visit http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

var mubsub = require('mubsub');

// Using a URI
var client = mubsub('mongodb://localhost:27017/mubsub_example', [options]);

// Passing a MongoDB driver `Db` instance directly.
var client = mubsub(new Db(...));

Channels

A channel maps one-to-one with a capped collection (Mubsub will create these if they do not already exist in the database). Optionally specify the byte size of the collection and/or the max number of documents in the collection when creating a channel.

WARNING: You should not create lots of channels because Mubsub will poll from the cursor position.

var channel = client.channel('foo', { size: 100000, max: 500 });

Options:

  • size max size of the collection in bytes, default is 5mb
  • max max amount of documents in the collection
  • retryInterval time in ms to wait if no docs are found, default is 200ms
  • recreate recreate the tailable cursor when an error occurs, default is true

WARNING: Don't remove collections with running publishers. It's possible for mongod to recreate the collection on the next insert (before Mubsub has the chance to do so). If this happens the collection will be recreated as a normal, uncapped collection.

Subscribe

var subscription = channel.subscribe([event], callback);

Subscriptions register a callback to be called whenever a document matching the specified event is inserted (published) into the collection (channel). You can omit the event to match all inserted documents. To later unsubscribe a particular callback, call unsubscribe on the returned subscription object:

subscription.unsubscribe();

Publish

channel.publish(event, obj, [callback]);

Publishing a document simply inserts the document into the channel's capped collection. A callback is optional.

Listen to events

The following events will be emitted:

// The given event was published
channel.on('myevent', console.log);

// Any event was published
channel.on('message', console.log);

// Document was inserted
channel.on('document', console.log);

// Mubsub is ready to receive new documents
channel.on('ready', console.log);

// Connection error
client.on('error', console.log);

// Channel error
channel.on('error', console.log);

Close

client.close();

Closes the MongoDB connection.

Install

npm install mubsub

Tests

make test

You can optionally specify the MongoDB URI to be used for tests:

MONGODB_URI=mongodb://localhost:27017/mubsub_tests make test

Projects using mubsub

  • simpleio Simple long polling based communication.

More Repositories

1

dandelion

Incremental Git repository deployment.
Ruby
737
star
2

backbone.io

Backbone.js sync via Socket.IO
540
star
3

data.io

Bidirectional data syncing via Socket.IO
JavaScript
387
star
4

backbone.viewkit

Backbone view management and transitions
JavaScript
214
star
5

monq

MongoDB-backed job queue for Node.js
JavaScript
185
star
6

queued

Simple HTTP-based queue server
Go
132
star
7

syphon

Immutable architecture for React/Flux applications
JavaScript
76
star
8

mongoose-acl

Mongoose ACL
JavaScript
61
star
9

jot

A mobile web app for your plain text notes
Clojure
34
star
10

bms

Battery management system for 4-series li-ion packs
C
34
star
11

cell-monitor

Battery cell monitor and balancer
C++
29
star
12

tree.js

JavaScript library for creating and manipulating hierarchical tree structures.
JavaScript
24
star
13

redblack.js

Red-black tree for Node.js and the browser
JavaScript
19
star
14

avr-twi

Nonblocking TWI/I2C master driver for Atmel AVR
C
18
star
15

cmcm

Cooperative multitasking for ARM Cortex-M microcontrollers
C
11
star
16

nettle

On-the-fly processing framework for Node.js and MongoDB
JavaScript
10
star
17

manacle.js

Lightweight ACL for Node.js and the browser
JavaScript
9
star
18

ina219

INA219 current/power monitor driver for Rust
Rust
5
star
19

pagoda

Reusable function stacks for Node.js and the browser
JavaScript
5
star
20

mongoose-denormalize

Bidirectional denormalization for your Mongoose models
JavaScript
5
star
21

emt

Express/Mongoose Toolkit
JavaScript
4
star
22

pack-monitor

Battery pack monitor
C++
4
star
23

blobstore

Content addressable blob store
Rust
4
star
24

cnc-tools

CNC tools for working with Gcode and Grbl
Python
3
star
25

require-hbt

An AMD loader plugin for Handlebars templates
JavaScript
3
star
26

broadcastd

Simple HTTP-based pubsub server
Go
3
star
27

modbus.cr

Modbus client library for Crystal
Crystal
2
star
28

gpio-socket

GPIO control via WebSocket
Clojure
2
star
29

stm32l0

Minimal development environment for STM32L0 using libopencm3
C
2
star
30

normal

Normalize nested data according to a relationship schema
Clojure
2
star
31

celeriac

State management for ClojureScript applications
Clojure
2
star
32

scthing

SuperCollider control interface for embedded devices
Rust
2
star
33

weedb

A wee database for Node.js built with LevelDB
JavaScript
2
star
34

routemachine

Stateful client-side router
JavaScript
2
star
35

node-queued

Queued client for Node.js
JavaScript
2
star
36

daffy

A minimal Scheme interpreter written in Python.
Python
2
star
37

splitd

Splits newline-separated HTTP response body into multiple HTTP requests
Go
2
star
38

peerpipe

An auto-discoverable, encrypted network pipe for P2P data transfer
Rust
1
star
39

raisin

A Python library for storing, retrieving and indexing JSON objects in MySQL.
Python
1
star
40

queued-ruby

Queued client for Ruby
Ruby
1
star
41

kite

Another lightweight Python web framework.
Python
1
star
42

terrain

Opinionated toolkit for building CRUD APIs with Rails
Ruby
1
star
43

earle

A URL pattern parser for Django.
Python
1
star
44

pytest-trace

Save OpenTelemetry spans generated during testing
Python
1
star
45

cello

An append-only B+ tree with multiversion concurrency control.
Scala
1
star
46

decibel

Networked audio player services
JavaScript
1
star
47

emacs.d

My Emacs config
Emacs Lisp
1
star
48

rfm69-gateway

RFM69 Linux gateway
C
1
star
49

webaudio

Experiments with the Web Audio API
JavaScript
1
star
50

pgque

PostgreSQL job queue extension using advisory locks
PLpgSQL
1
star