1.1.68 • Published 3 years ago

pubsocket v1.1.68

Weekly downloads
12
License
ISC
Repository
-
Last release
3 years ago

PubSocket

Javascript implementation of the Publish/Subscribe pattern using socket.io.

Install

npm

npm install pubsocket

yarn

yarn add pubsocket

Example Usage

Server

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.

Client

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.

Special Thanks

insecure Leonardo Holanda help with testing and documentation.

1.1.68

3 years ago

1.1.67

3 years ago

1.1.66

3 years ago

1.1.65

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago