-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: websocket for GetMessages (#122)
* Feat: user can't retrieve data from Aleph through webSocket Solution: Add a websocket implementation * Fix: Replace array mutation by new array in toolshade * Feat: Add webSocket implementation for Nodejs with tests * Feat: Add the history parameter
- Loading branch information
Showing
10 changed files
with
383 additions
and
15 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
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,66 @@ | ||
import { useState } from "react"; | ||
import { GetMessagesSocket } from "../../../../src/messages/any"; | ||
import {AlephSocket, SocketResponse} from "../../../../src/messages/any/getMessagesSocket"; | ||
|
||
function WebSocket() { | ||
const [socket, setSocket] = useState<AlephSocket | undefined>() | ||
const [data, setData] = useState<SocketResponse[]>([]); | ||
|
||
const startSocket = async () => { | ||
const newSocket = GetMessagesSocket({}); | ||
|
||
setSocket(newSocket); | ||
} | ||
|
||
const displayContent = async () => { | ||
if (!socket) return; | ||
setData([...socket.getData()]); | ||
} | ||
|
||
const clearSocket = async () => { | ||
socket?.clearData(); | ||
await displayContent(); | ||
} | ||
|
||
const closeSocket = async () => { | ||
socket?.closeSocket() | ||
setSocket(undefined); | ||
} | ||
|
||
return ( | ||
<> | ||
<div style={{display: 'flex', flexWrap:'wrap', gap: '6px'}}> | ||
<button onClick={startSocket} disabled={!!socket}>Open WebSocket</button> | ||
<button onClick={displayContent} disabled={!socket}>Display</button> | ||
<button onClick={clearSocket} disabled={!socket}>Clear Data</button> | ||
<button onClick={closeSocket} disabled={!socket}>Close Socket</button> | ||
</div> | ||
|
||
<p>Total: {data.length}</p> | ||
<div> | ||
{ !!socket ? (<table style={{marginTop: '2em'}}> | ||
<thead> | ||
<tr> | ||
<th>Sender</th> | ||
<th>Chain</th> | ||
<th>Hash</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{data.map((line, id) => { | ||
return ( | ||
<tr key={id}> | ||
<td>{`${line.sender.substring(0, 6,)}...${line.sender.substring(line.sender.length - 4,)}`}</td> | ||
<td>{line.chain}</td> | ||
<td>{line.item_hash}</td> | ||
</tr> | ||
) | ||
})} | ||
</tbody> | ||
</table>) : (<p>Socket is closed</p>)} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default WebSocket |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export const DEFAULT_API_V2 = "https://api2.aleph.im"; | ||
export const DEFAULT_API_WS_V2 = "ws://api2.aleph.im"; |
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,66 @@ | ||
import { GetMessagesSocketParams, SocketResponse } from "./getMessagesSocket"; | ||
import WebSocket from "ws"; | ||
|
||
/** | ||
* This class is used to manipulate Node Web Socket to list Aleph Messages | ||
*/ | ||
export class AlephNodeWebSocket { | ||
private readonly socket: WebSocket; | ||
private data: SocketResponse[]; | ||
|
||
private isOpen: boolean; | ||
|
||
constructor(queryParam: GetMessagesSocketParams, apiServer: string) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const self = this; | ||
this.isOpen = false; | ||
this.data = []; | ||
let queryParamString = ""; | ||
|
||
Object.entries(queryParam).forEach(([key, value]) => { | ||
if (!!value) queryParamString = queryParamString + `&${key}=${value}`; | ||
}); | ||
if (!!queryParamString) queryParamString = queryParamString.substring(1); | ||
this.socket = new WebSocket(`${apiServer}/api/ws0/messages?${queryParamString}`); | ||
|
||
// ON OPEN SOCKET | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
this.socket.on("open", function open() { | ||
console.log("[Aleph-NodeWebSocket] Connection established"); | ||
self.isOpen = true; | ||
}); | ||
|
||
// ON RECEIVE DATA | ||
this.socket.on("message", function message(data) { | ||
self.data.push(JSON.parse(data.toString())); | ||
}); | ||
|
||
// ON CLOSE SOCKET | ||
this.socket.on("close", function close() { | ||
console.log(`[Aleph-NodeWebSocket] Connection closed`); | ||
self.isOpen = false; | ||
}); | ||
|
||
// ON ERROR | ||
this.socket.on("error", console.error); | ||
} | ||
|
||
public getData = (): SocketResponse[] => { | ||
return this.data; | ||
}; | ||
public getSocket = (): WebSocket => { | ||
return this.socket; | ||
}; | ||
|
||
public getIsOpen = (): boolean => { | ||
return this.isOpen; | ||
}; | ||
|
||
public clearData = (): void => { | ||
this.data = []; | ||
}; | ||
|
||
public closeSocket = (): void => { | ||
this.socket.close(1000, "Work complete"); | ||
}; | ||
} |
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,69 @@ | ||
import { GetMessagesSocketParams, SocketResponse } from "./getMessagesSocket"; | ||
|
||
/** | ||
* This class is used to manipulate Web Socket to list Aleph Messages | ||
*/ | ||
export class AlephWebSocket { | ||
private readonly socket: WebSocket; | ||
private data: SocketResponse[]; | ||
|
||
private isOpen: boolean; | ||
|
||
constructor(queryParam: GetMessagesSocketParams, apiServer: string) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const self = this; | ||
this.isOpen = false; | ||
this.data = []; | ||
let queryParamString = ""; | ||
|
||
Object.entries(queryParam).forEach(([key, value]) => { | ||
if (!!value) queryParamString = queryParamString + `&${key}=${value}`; | ||
}); | ||
if (!!queryParamString) queryParamString = queryParamString.substring(1); | ||
this.socket = new WebSocket(`${apiServer}/api/ws0/messages?${queryParamString}`); | ||
|
||
// ON OPEN SOCKET | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
this.socket.onopen = function (_) { | ||
console.log("[Aleph-webSocket] Connection established"); | ||
self.isOpen = true; | ||
}; | ||
|
||
// ON RECEIVE DATA | ||
this.socket.onmessage = function (event) { | ||
self.data.push(JSON.parse(event.data)); | ||
}; | ||
|
||
// ON CLOSE SOCKET | ||
this.socket.onclose = function (event) { | ||
self.isOpen = false; | ||
if (event.wasClean) | ||
console.log(`[Aleph-webSocket] Connection closed cleanly, code=${event.code} reason=${event.reason}`); | ||
else console.log("[Aleph-webSocket] Connection died"); | ||
}; | ||
|
||
// ON ERROR | ||
this.socket.onerror = function (error) { | ||
console.log("[Aleph-webSocket]: error: ", error); | ||
}; | ||
} | ||
|
||
public getData = (): SocketResponse[] => { | ||
return this.data; | ||
}; | ||
public getSocket = (): WebSocket => { | ||
return this.socket; | ||
}; | ||
|
||
public getIsOpen = (): boolean => { | ||
return this.isOpen; | ||
}; | ||
|
||
public clearData = (): void => { | ||
this.data = []; | ||
}; | ||
|
||
public closeSocket = (): void => { | ||
this.socket.close(1000, "Work complete"); | ||
}; | ||
} |
Oops, something went wrong.