-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nearup-io/event-listeners
Nodb event listeners
- Loading branch information
Showing
4 changed files
with
124 additions
and
14 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import WebSocket from "ws"; | ||
import { NodbError } from "./errors"; | ||
|
||
class NodbEventListener { | ||
protected socket: WebSocket | undefined; | ||
|
||
protected connect(props: { | ||
baseUrl: string; | ||
appName: string; | ||
envName?: string; | ||
token: string; | ||
}) { | ||
if (!props.token) { | ||
throw new NodbError("Token is missing!"); | ||
} | ||
const envUrlPart = props.envName ? `/${props.envName}` : ""; | ||
|
||
const formattedBaseUrl = props.baseUrl | ||
.replace("http://", "ws://") | ||
.replace("https://", "wss://"); | ||
this.socket = new WebSocket( | ||
`${formattedBaseUrl}/ws/${props.appName}${envUrlPart}`, | ||
{ | ||
headers: { | ||
token: props.token, | ||
}, | ||
}, | ||
); | ||
|
||
this.socket.on("open", () => { | ||
console.log("Connected to socket"); | ||
}); | ||
|
||
this.socket.onerror = () => { | ||
throw new NodbError(`Something went wrong with socket!`); | ||
}; | ||
|
||
this.listenForMessages(); | ||
} | ||
|
||
protected listenForMessages() { | ||
this.socket?.on("message", (data) => { | ||
const message = data.toString(); | ||
try { | ||
const { | ||
type, | ||
appName, | ||
envName, | ||
data: messageData, | ||
} = JSON.parse(message) as { | ||
type: string; | ||
appName: string; | ||
envName: string; | ||
data: any; | ||
}; | ||
console.log(`Operation ${type.toUpperCase()}`); | ||
console.log( | ||
`Affected environment: ${JSON.stringify({ appName, envName }, null, 2)}`, | ||
); | ||
console.log(`Data: ${JSON.stringify(messageData, null, 2)}`); | ||
} catch (err) { | ||
console.error("Error parsing JSON:", err); | ||
} | ||
}); | ||
} | ||
|
||
public disconnectFromSocket() { | ||
this.socket?.close(); | ||
} | ||
} | ||
|
||
export default NodbEventListener; |
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