Socket
π Socket Module
The socket
module in the ono-web package provides a WebSocket client to subscribe and listen to real-time updates from the ONO core node network.
π¦ Importing
To use the socket module, import it as follows:
π Class: OnoSocketClient
OnoSocketClient
The OnoSocketClient
class allows you to connect, subscribe to events, and receive real-time updates from the ONO blockchain network.
π¨ Constructor
new OnoSocketClient(coreHost?)
new OnoSocketClient(coreHost?)
Creates a new instance of the socket client.
If no coreHost
is provided, it defaults to ws://core.ono.gg
.
π₯ Parameters
coreHost
string
Optional
Custom ONO core node WebSocket address (e.g., ws://127.0.0.1:5001
)
π Example
π‘ Methods
1. subscribe(handler, errorHandler)
subscribe(handler, errorHandler)
Connects to the WebSocket server and subscribes to real-time blockchain updates (like new blocks, transactions, etc.).
π₯ Parameters
handler
function
Callback that handles incoming messages. Receives (message, socketClient)
errorHandler
function
Callback to handle JSON parsing or connection errors. Receives (error)
β
Behavior
Establishes a WebSocket connection.
Automatically manages connection open/close state.
Calls
handler
when a message is received.Calls
errorHandler
if thereβs an error processing a message.
π Example
2. setCoreHost(address)
setCoreHost(address)
Changes the WebSocket core host after initialization, allowing reconnection to a different node.
π₯ Parameters
address
string
New WebSocket address for core node (e.g., http://127.0.0.1:5001
)
π Example
3. disconnect()
disconnect()
Gracefully closes the WebSocket connection and unsubscribes from updates.
π Example
π Usage Example (Full Flow)
βοΈ Internal Mechanics
Each client instance gets a unique
node-id
using UUID, passed as query parameter for identification:ws://core.ono.gg?node-id=generated-uuid
Automatically manages:
Connection opening
Re-subscription protection
Message parsing and error handling
π¨ Notes
Make sure your core node allows external WebSocket connections if using a remote or custom host.
Handle sensitive events securely when working with
/secure
WebSocket updates.Always manage
disconnect()
properly to avoid memory leaks or dangling connections.
Last updated