New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tcppubsub

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tcppubsub

A simple node-js-tcp publish-subscribe framework. With a broker and a client called member.

npmnpm
Version
1.0.0
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

tcppubsub

A simple node-js-tcp publish-subscribe-request-response framework :octopus:. With a broker and a client called member.

A simple and fast :leopard: exchange of data between nodejs-applications.Use the publish-subscribe-pattern to handle events or use the request-response-pattern to query or serve some data.

  • Go to broker
  • Go to member

Build Status

Installing

npm i tcppubsub

Broker :octopus:

A broker handles all data from the members, like sockets, topics and payload. You can use some events do handle the member-data directly at the broker-side.

var tcppubsub = require('tcppubsub')

var port = 2223
var host = 'localhost'
var blockPublisher = false // block payload sending to publisher, if he has subscribed the topic too

var broker = new tcppubsub.Broker(port, host, blockPublisher)
broker.listen() 

broker.getConnections(function(err, numb){
    console.log('connected members:', numb)
})

//use the socket-events like:
broker.on('end', function(msg){console.log('end:', msg)})
broker.on('close', function(member){console.log('close:', msg)})
broker.on('error', function(err){console.log('error:', err)})
broker.on('published', function(data){console.log('published:', data)})
broker.on('subscribed', function(topic){console.log('subscribed:', topic)})
broker.on('unsubscribed', function(topic){console.log('unsubscribed:', topic)})

Member :leopard:

  • Publish-subscribe data.
  • Listen on requests.
  • Request some data from listeners.
  • Topic without wildcard 'app/configuration/server' .
  • Topic with wildcard 'app/configuration/#' or 'app/#/configuration',....
  • Data can be a string or a object and is emitted as a buffer.
  • When the broker restarting, it will automatically resubscribe all topics from subscription or listeners.

Connect

Call the connect-method to connect the member.

member.connect(function(){
    // Use other methods like sub, unsub, pub, req, listen in here.
})

Subscribe

Subscribe a topic. For multiple topics give a array of topics like ['topic1', 'topic2',...].

member.sub(topic, function(topic){})

Unsubscribe

Unsubscribe a topic. For multiple topics give a array of topics like ['topic1', 'topic2',...].

member.unsub(topic)

Publish

Publish some data on a topic.

member.pub(topic, data)

Request

Make a request on a listener topic. Set some timeout in ms for handle timeout-errors. Default-timeout: 10min.

member.req(topic, data, function(err, data, id){
    if(err){
        console.log(err) // timeout error
    }else{
        console.log(data)
    }
}, timeout)

Listen

Listen on a specific topic for requests. Handle the data and send the response with res.

member.listen(topic, function(data, res){
    console.log(data)
    res(data)
})

Example

var tcppubsub = require('tcppubsub')

var port = 2223
var host = 'localhost'

//Create the member
var member = new tcppubsub.Member(port, host)


member.connect(function(){

    //Subscribe the topic without callback
    member.sub('app/configuration/service')

    // Subscribe the topic STRING or ARRAY
    member.sub('app/configuration/server', function(topic){

        // Publish some data STRING, OBJECT, ARRAY
        member.pub('app/configuration/server', {name: 'yamigr', b : 'Hello World'})
    })


    // Receive the data on the certain topic
    member.on('app/configuration/server', function(data){

        // Unsubscribe the topic
        member.unsub('app/configuration/server')
        console.log('rcv publish:', data)
    })

    /******* OR *******/

    // Receive all publishes on subscribed data
    member.on('message', function(topic, data){
        member.unsub('app/configuration/server')
        console.log(topic, data)
    })


    member.listen('app/static/config', function(data, res){
        res({ msg: 'Hello ' + data.name})
    })


    let timeout = 2000 // default 5000

    member.req('app/static/config', { name : 'Peter Pan'}, function(err, data, id){
        if(!err){
            console.log('rcv response:', data.msg)
        }
    }, timeout)
})



Authors

  • Yannick Grund - Initial work - yamigr

ToDo

  • Benchmark tests (broker and members)

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

tcppubsub

FAQs

Package last updated on 12 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts