Create your own PubSub Client & Server use WebSocketโ€‚by@tabvn

Create your own PubSub Client & Server use WebSocket

June 23rd 2022 11,632 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
image
Tabvn HackerNoon profile picture

Tabvn

How it worksย ?




const pubSub = new PubSubClient('ws://localhost:3001', {connect: true,reconnect: true,})
const topicName = 'abc'  

pubSub.subscribe(topicName, (message) => {  
  console.log(\`Got message from topic ${topicName}\`, message)  
})  

//publish a message to topic  
pubSub.publish(topicName,  
  {title: 'Hello subscribers in the topic abc', body: 'How are you ?'})  

// Broadcast send message to subscribers but not me  
pubSub.broadcast(topicName, {body: 'this is broadcast message'})

// UnsubscribepubSub.unsubscribe(topicName)

[email protected]

  • May limit publisher and time to send message if look like spammer
  • Maybe server need wait a time (short time) if detect mutiple publish messages same to one client and send a batch of messages instead of send a single message.
  • Add Crypto client & server key sharing to decrypt between client & server when connected. More security for WebSocket communication. The message from client to server and server to client is encrypted.

Part 1: WebSocket Client and WebSocket Server

image

Part 2: Subscriptionย : Publish, Subscribe, Broadcast

image

Part 3: Implement auto reconnect

image

Part 4: Handle Unsubscribe

image

Demo

image
Video Playlist
react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!