-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Systest 9779 #137
Open
kdivya153
wants to merge
7
commits into
dev
Choose a base branch
from
SYSTEST-9779
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Systest 9779 #137
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a95fb7
changes to support simplePubSub
kdivya153 c0bc33d
Fixed Lint errors
kdivya153 30f757b
updated pubsub.md
kdivya153 19fa86e
updated logger.error to info
kdivya153 5fac783
Add pubSubUrl support to URL/Intent
kschrief 3b1384c
Checking for env pub sub url only when we go to initialize the pubSub…
jbigel277 4f6a41d
setting up pubSub topic with appId
jbigel277 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,16 +15,15 @@ | |
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
const logger = require('../src/utils/Logger')('pubSubClient.js'); | ||
require('dotenv').config({ override: true }); | ||
|
||
const defaultWsUrl = 'ws://your-ws-url-here.com'; | ||
const defaultWsUrl = 'ws://localhost:8080'; | ||
|
||
class PubSubClient { | ||
constructor() { | ||
constructor(url = defaultWsUrl) { | ||
this.ws = null; | ||
this.url = defaultWsUrl; | ||
this.url = url; | ||
this.PUBSUB_SUBSCRIBE_TOPIC_SUFFIX = '_FCS'; | ||
this.PUBSUB_PUBLISH_TOPIC_SUFFIX = '_FCA'; | ||
} | ||
|
@@ -56,7 +55,7 @@ class PubSubClient { | |
|
||
// Establish WS Connection | ||
this.ws = new WebSocket(this.url); | ||
logger.info('Establishing a WS connection...', 'initialize'); | ||
logger.info(`Establishing a WS connection to ${this.url}...`, 'initialize'); | ||
|
||
return new Promise((resolve, reject) => { | ||
this.ws.addEventListener('open', (event) => { | ||
|
@@ -65,13 +64,13 @@ class PubSubClient { | |
}); | ||
|
||
this.ws.addEventListener('error', (event) => { | ||
if (this.url === defaultWsUrl) { | ||
logger.error('WARNING: WebSocket connections will fail to initialize. The file has not been properly configured. Please update the URL to point to your WebSocket server for communication to work.'); | ||
} else { | ||
logger.error('Failed to initialize a WS connection...', 'initialize'); | ||
} | ||
logger.error('Failed to initialize a WS connection...', event); | ||
this.ws = null; // Ensure ws is null if connection fails | ||
reject(false); | ||
}); | ||
}).catch((error) => { | ||
logger.error('Continuing without PubSub due to WS connection failure.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above RE: "info" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
return false; | ||
}); | ||
} | ||
|
||
|
@@ -82,6 +81,11 @@ class PubSubClient { | |
return false; | ||
} | ||
|
||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { | ||
logger.error('WS connection is not open. Cannot publish message.'); | ||
return false; | ||
} | ||
|
||
const publishMsg = { | ||
operation: 'pub', | ||
topic, | ||
|
@@ -92,7 +96,7 @@ class PubSubClient { | |
|
||
// If headers are passed in, add them to the payload object | ||
if (headers) { | ||
payload.payload.headers = headers; | ||
publishMsg.payload.headers = headers; | ||
} | ||
|
||
logger.info('Publishing message: ', JSON.stringify(publishMsg)); | ||
|
@@ -109,6 +113,11 @@ class PubSubClient { | |
|
||
// Subscribe to a topic | ||
subscribe(topic, callback) { | ||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { | ||
logger.error('WS connection is not open. Cannot subscribe to topic.'); | ||
return false; | ||
} | ||
|
||
const subscribeMsg = { | ||
operation: 'sub', | ||
topic, | ||
|
@@ -146,6 +155,11 @@ class PubSubClient { | |
|
||
// Unsubscribe to a topic | ||
unsubscribe(topic) { | ||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { | ||
logger.error('WS connection is not open. Cannot unsubscribe from topic.'); | ||
return false; | ||
} | ||
|
||
const payload = { | ||
operation: 'unsub', | ||
topic, | ||
|
@@ -164,17 +178,21 @@ class PubSubClient { | |
// Checks WebSocket connection status | ||
isConnected() { | ||
let status = false; | ||
if (this.ws && this.ws.readyState == this.ws.OPEN) { | ||
logger.info('WS connection already Established'); | ||
if (this.ws && this.ws.readyState === WebSocket.OPEN) { | ||
logger.info('WS connection already established'); | ||
status = true; | ||
} | ||
return status; | ||
} | ||
} | ||
|
||
const getClient = async () => { | ||
const pubSubClient = new PubSubClient(); | ||
await pubSubClient.initialize(); | ||
const getClient = async (url = defaultWsUrl) => { | ||
const pubSubClient = new PubSubClient(url); | ||
try { | ||
await pubSubClient.initialize(); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
return pubSubClient; | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave this as "info". It will only technically be an error if someone wants to connect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed