Javascript implementation of the Publish/Subscribe pattern using socket.io.
npm
npm install pubsocket
yarn
yarn add pubsocket
const {PubSocket} = require('pubsocket');
// Create PubSocket server instance
const pubSocket = PubSocket();
// Create a channel for this pubsocket
const channel = pubSocket.createChannel('ROOM');
// Add Listener to receive messages
channel.addListener((data) => {console.log(data)});
// Publish a message for all connected peers
channel.publish("Server Message: Hi.");
setTimeout(() => {
// Disconnect the channel
channel.disconnect();
// Close PubSocketServer
pubSocket.close();
}, 10000);
The default port for the server is 3000.
const {PubSocketClient} = require('pubsocket');
// Create PubSocket client instance
const psClient = PubSocketClient();
// Connect the client
psClient.connect('http://localhost:3000', 'ROOM').then(() => {
// Add Listener to receive messages
psClient.addListener((data) => {console.log(data)});
// Publish a message for channel connected peers
psClient.publish('Client Message: Hi');
setTimeout(() => {
// Disconnect the client
psClient.disconnect();
}, 10000);
});
The client need to connect to a server, if you not define the serverUrl property the Client admit http://localhost:3000/ by default.
The Channel Name is required.
Leonardo Holanda
help with testing and documentation.