Solana websocket RPC 与 @solana/web3.js

问题描述 投票:0回答:3

我正在尝试使用 websockets 连接到 Solana 主网。我不知道如何使用 web3.js 进行连接。也许有人遇到这个问题并可以帮助我?

谢谢

这是我编写的一行简单代码:

let con = new web3.Connection('https://api.mainnet-beta.solana.com', { commitment: "confirmed", wsEndpoint: 'ws://api.mainnet-beta.solana.com' });
websocket solana solana-web3js
3个回答
3
投票

查看 Solana Cookbook 上使用 web3 订阅 websockets 的示例:https://solanacookbook.com/references/local-development.html#subscribing-to-websocket


2
投票
function monitor() {
    ws = new WebSocket(WSS_ENDPOINT)
    ws.onopen = () => {
        ws.send(
            JSON.stringify({
                jsonrpc: '2.0',
                id: 1,
                method: 'programSubscribe',
                params: [
                    address,
                    {
                        encoding: 'base64',
                        commitment: 'finalized',
                    },
                ],
            })
        )
    }

您要使用的端点位于该方法条目中。

现在每次你想对某件事做出反应......你都会使用

 ws.on('message', (evt) => {
        try {
            const buffer = evt.toString('utf8')

            console.log(buffer)
        } catch (e) {
            console.log(e)
        }
    })
}

0
投票

尝试 Bitquery 的 Solana 流。

这是例子

获取最新余额更新 - https://ide.bitquery.io/Solana-Balance-Updates

实时跟踪 NFT 余额更新 - https://ide.bitquery.io/Solana-NFT-Balance-Updates

订阅最近交易 - https://ide.bitquery.io/Realtime-Solana-Transactions

在 Solana Raydium DEX 上创建新的流动性池 - https://ide.bitquery.io/Latest-Radiyum-V4-pools-created_1

订阅最新的 Solana 交易 - https://ide.bitquery.io/Get-Latest-Solana-DEX-Trades-in-Realtime

查看文档以了解更多信息 - https://docs.bitquery.io/docs/category/solana/

© www.soinside.com 2019 - 2024. All rights reserved.