@sopherapps/simplemq v0.0.4
simplemq
A simple message queue server that requires only nodejs. It is can be used to pass JSON/string messages from one application to another in an asynchronous way across the network.
Do note that this package is still under heavy development
Components
This package includes:
- A message queue server
- A message queue client
One can chose to use any or both of these two components.
Major Dependencies
Getting Started
Running the simplemq Server
Create a nodejs project
npm init -yInstall simplemq
npm install @sopherapps/simplemqCreate the server file. Let's name it
server.jstouch server.jsIn
server.jsfile, import the Server component fromsimplemq, initialize it then call itsstartmethod, and itsstopmethod to stop the server in case of an error e.g. a KeyboardInterrupt Theoptionspassed on initiliazation include:- the
portto run on - the
ttli.e. time to live in milliseconds for the messages before they are considered stale - the
ttlIntervali.e. the interval in milliseconds for clearing out stale messages - the
streamIntervali.e. the interval at which messages are to be sent to any listening client - the
dbFilePathi.e. the base path to the leveldb database to persist the messages, subscribers and topics - the
maxWaitBeforeForcedShutDowni.e. the number of milliseconds to wait after a shutdown has been initiated for a forceful shutdown to come into action
const {Server} = require('simplemq'); const server = new Server({ port: 38000, // Default 38000 ttl: 1000 * 60 * 60 * 24 * 30, // Default 30 days ttlInterval: 1000 * 60 * 60 * 24, // Default 1 day ... }); try { server.start(); } catch { if(server){ server.stop(); } }- the
Run the nodejs server script
node server.js
Connecting to a simplemq Server
In your nodejs project, install simplemq
npm install @sopherapps/simplemqCreate the client file if you don't have one yet. Let's call it
client.jsIn
client.js, import the Client component fromsimplemqand initialize it withoptionsTheconfigon initialization specifies:- the IP address (
ipAddress) of the simplemq server - the optional
porton which the simplemq server is running. Default is 38000. - the optional
intervalin milliseconds at which to receive the messages. Default is 1000. - a random
clientIdto identify the client
const {Client} = require('@sopherapps/simplemq'); const client = new Client({ ipAddress: 'localhost', // the ip address, for now we will assume the server is on this computer port: 38000, // Default is 38000, the port as specified in the server code interval: 1000, // Default is 1000, receive messages at least every second clientId: 'ity65476t9ygyf', // some random identifier the server will use to identify this client everytime the client connects });- the IP address (
To connect to the remote server, call the
connect()method of the client. Any attempt to generate another producer or listener from the client before it is connected will throw an exception.client.connect();Then call the client's
getMessageProducermethod to get a message producer, with anoptionsargument specifying:- the
topic- the topic to send to - the optional
onError- the error handler function - the
onMessage- the message handler
const messageProducer = client.getMessageProducer({ topic: 'Some topic', onError: (err)=>{console.error(err);} onMessage: (message)=>{console.log(message);} });- the
To send messages to the selected topic, repeatedly call the
sendmethod of the message producer instance, while providing the data in JSON form that is to be sentmessageProducer.send(JSON.stringify({hello: 'haloha'})); messageProducer.send(JSON.stringify({bye: 'good bye'}));Or
getMessageListenermethod to get a message listener with anoptionsargument specifying:- the
topicthat is to be listened to - the
onMessagehandler to be called whenever a message is received - the optional
onSubscriptionhandler to be called when a client succesfully subscribes to the given topic - the optional
onStophandler to be called when a client stops listening - the optional
onStarthandler to be called when the client starts listening for messages
const messageListener = client.getMessageListener({ topic: 'Some topic', onError: (err) => { throw err; }, onMessage: (message) => { console.log(message); }, onSubscription: (topicName) => { console.log(`Subscribed to ${topicName}`); }, onStop: () => { console.log(`Stopped listening on ${client.ipAddressAndPort}`); }, onStart: () => { console.log(`Listening on ${client.ipAddressAndPort} to Topic: ${topic}`); }, });- the
To start listening for messages, call the
startmethod of the message listener instancemessageListener.start();To stop listening for messages, call the
stopmethod of the message listener instancemessageListener.stop();To disconnect the client from the remote server, call the
disconnect()method of the client instance. Any attempt to generate another producer or listener from the client after it has been disconnected will throw an exception.client.disconnect();
How To Contribute
Coming soon.
How to test
In the root of the project, install the dependencies
npm installRun the test command
npm test
ToDo
- Make leveldb as persistent store
- Modularize the DB module itself into TOPICS, MESSAGES AND SUBSCRIBERS
- Add JavaScript client code
- Create python client package
- Create java client package
- Add option to add certificates for authentication
- Add option for broadcast messages that don't get persisted
- Create sample app using simplemq
- Create blog posts showing its use
- Create videos showing its use
- Create a cloud service for simplemq
- Add logger
- Publish npm package
- Publish pypi package
License
Copyright (c) 2021 Martin Ahindura Licensed under the MIT License