Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

nicocoul/ya-pubsub

Repository files navigation

Ya PubSub is a topic-based publish/subscribe library for Node js.

Works well with ya-rfc.

Key Features

  • asynchronous
  • embeddable
  • designed for micro-services

Examples

Publishing and subscribing locally (same process)

const pubsub = require('../index.js')

const broker = pubsub.broker()

broker.publish('some-topic', 'hello')

broker.subscribe('some-topic', (message) => {
  console.log('received from first subscriber', message)
})

broker.subscribe('some-topic', (message) => {
  console.log('received from second subscriber', message)
})

setTimeout(() => {
  broker.publish('some-topic', { hello: 'world2' })
}, 50)

/* output:
received from first subscriber hello
received from second subscriber hello
received from first subscriber { hello: 'world2' }
received from second subscriber { hello: 'world2' }
*/

Publishing and subscribing over tcp

const net = require('net')
const ya = require('../index.js')

const broker = ya.broker()
broker.plug(ya.plugins.net(net.Server().listen(8000)))

broker.publish('some-topic', 'hello')

const client1 = ya.client.net({ host: 'localhost', port: 8000 })
const client2 = ya.client.net({ host: 'localhost', port: 8000 })

client1.publish('some-topic', { hello: 'world1' })

client2.subscribe('some-topic', (message) => {
  console.log(message)
})

setTimeout(() => {
  client1.publish('some-topic', { hello: 'world2' })
}, 50)

/* output:
hello
{ hello: 'world1' }
{ hello: 'world2' }
*/

Publishing and subscribing over web sockets

const { WebSocketServer } = require('ws')
const ya = require('../index.js')

const broker = ya.broker()
broker.plug(ya.plugins.ws(new WebSocketServer({ port: 8001 })))
broker.publish('some-topic', 'hello')

const client1 = ya.client.ws({ host: 'localhost', port: 8001 })
const client2 = ya.client.ws({ host: 'localhost', port: 8001 })

client2.publish('some-topic', { hello: 'world1' })

client2.subscribe('some-topic', (message) => {
  console.log(message)
})

setTimeout(() => {
  client1.publish('some-topic', { hello: 'world2' })
}, 50)

/* output:
hello
{ hello: 'world1' }
{ hello: 'world2' }
*/

Topic Filtering

Versioning

Yaps-node uses Semantic Versioning for predictable versioning.

Alternatives

These are a few alternative projects that also implement topic based publish subscribe in JavaScript.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published