Skip to content

Subscribing to less common socket events in v6 (i.e. newHeads) #3949

Open
@Stanback

Description

@Stanback

Suggestion

It would be helpful to have more clarity/examples in the documentation regarding event subscriptions for the lesser used web socket events, for example newHeads and newPendingTransactions.

In v5, I would do it like so:

const provider = new WebSocketProvider(...)
provider._subscribe('newHeads', ['newHeads'], (result) => {
  const block = provider.formatter.block(result)
  console.log(block)
}

In v6 (currently 6.2.3), I'm able to get it working via the following:

const provider = new WebSocketProvider(...)
const sub = new SocketBlockSubscriber(provider)
sub._handleMessage = (result) => {
  const block = provider._wrapBlock({ ...result, transactions: [] }, provider._network)
  console.log(block)
}
provider._register('newHeads', sub)
sub.start()

Am I doing it right?

I'm not sure what the implications are, but seems like it would be convenient if we could pass custom subscriptions directly into provider.on('event', () => ...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationDocumentation related issue.enhancementNew feature or improvement.minor-bumpPlanned for the next minor version bump.on-deckThis Enhancement or Bug is currently being worked on.v6Issues regarding v6

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions