Skip to content

Commit

Permalink
v1.0.1 - websocket improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Oct 3, 2024
1 parent e63968a commit 38bf4ce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jackallabs/banshee",
"version": "1.0.0",
"version": "1.0.1",
"description": "Modern problems require modern solutions",
"keywords": [],
"exports": {
Expand Down
88 changes: 52 additions & 36 deletions src/classes/websocketCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { tidyString } from '@/utils/misc'

export class WebsocketCore implements IWebsocketCore {
protected readonly wsConnections: Record<string, WebSocket>
protected readonly wsFeeds: Record<string, TPossibleTxEvents[]>

constructor() {
this.wsConnections = {}
this.wsFeeds = {}
}

monitor<T extends TPossibleTxEvents>(
Expand All @@ -30,45 +32,59 @@ export class WebsocketCore implements IWebsocketCore {
protected setupMonitoring<T extends TPossibleTxEvents>(
conn: IIbcEngageBundle<T>,
): void {
if (
!conn.endpoint.startsWith('ws://')
&& !conn.endpoint.startsWith('wss://')
) {
throw new Error('invalid url')
}
const cleanEndpoint = tidyString(conn.endpoint, '/')
const finalEndpoint = cleanEndpoint.endsWith('websocket')
? cleanEndpoint
: `${cleanEndpoint}/websocket`
if (!this.wsConnections[conn.chainId]) {
this.wsConnections[conn.chainId] = new WebSocket(finalEndpoint)
}
const client = this.wsConnections[conn.chainId]
try {
if (
!conn.endpoint.startsWith('ws://')
&& !conn.endpoint.startsWith('wss://')
) {
throw new Error('invalid url')
}

const wsQuery = {
jsonrpc: '2.0',
method: 'subscribe',
id: Date.now().toString(),
params: {
query: conn.query
? `tm.event = 'Tx' AND ${conn.query}`
: `tm.event = 'Tx'`,
},
}
client.onopen = () => {
client.send(JSON.stringify(wsQuery))
}
client.onmessage = (msg) => {
try {
const data = JSON.parse(msg.data)
if (!data.result.data) {
return
const cleanEndpoint = tidyString(conn.endpoint, '/')
const finalEndpoint = cleanEndpoint.endsWith('websocket')
? cleanEndpoint
: `${cleanEndpoint}/websocket`
if (!this.wsConnections[conn.chainId]) {
this.wsConnections[conn.chainId] = new WebSocket(finalEndpoint)
}
const client = this.wsConnections[conn.chainId]

const query = conn.query
? `tm.event = 'Tx' AND ${conn.query}`
: `tm.event = 'Tx'`
const marker = Date.now().toString()
this.wsFeeds[`${marker}|${query}`] = conn.feed
const wsQuery = {
jsonrpc: '2.0',
method: 'subscribe',
id: marker,
params: { query },
}

if (client.readyState === 1) {
client.send(JSON.stringify(wsQuery))
} else {
client.onopen = () => {
client.send(JSON.stringify(wsQuery))
}
}

client.onmessage = (msg) => {
try {
const data = JSON.parse(msg.data)
if (!data.result.data) {
return
}

const ready = Responses.decodeTxEvent(data.result) as T
this.wsFeeds[`${marker}|${data.result.query}`].push(ready)
this.wsFeeds[`${marker}|${data.result.query}`] = []
} catch (err) {
console.error(err)
}
const ready = Responses.decodeTxEvent(data.result) as T
conn.feed.push(ready)
} catch (err) {
console.error(err)
}
} catch (err) {
throw err
}
}
}

0 comments on commit 38bf4ce

Please sign in to comment.