firehose

package
v0.0.0-...-8c7fadb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 7 Imported by: 0

README

Bluesky Firehose

Package firehose implements a client for the Bluesky Firehose API, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-bsky/firehose

GoDoc

Examples

package main

import (
	"fmt"
	"os"

	"github.com/gorilla/websocket"
	"github.com/reiver/go-bsky/firehose"
)

func main() {

	// The Bluesky Firehose API use WebSockets.
	//
	// This is the URL that we will connect to it.
	const uri string = firehose.WebSocketURI

	// Connect to the WebSocket.
	conn, _, err := websocket.DefaultDialer.Dial(uri, http.Header{})
	if nil != err {
		fmt.Fprintf(os.Stderr, "ERROR: could not connect to Bluesky Firehose API at %q: %s \n", uri, err)
		return
	}
	defer conn.Close() // <-- we need to eventually close the WebSocket, so that we don't have a resource leak.

	// A WebSocket returns a series of messages.
	//
	// Here we loop, read each message from the WebSocket one-by-one.
	for {
		// Here we are just getting the raw binary data.
		// Later we decode it.
		wsMessageType, wsMessage, err := conn.ReadMessage()
		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: problem reading from WebSocket for the connection to the Bluesky Firehose API at %q: %s \n", uri, err)
			return
		}

		// Technically a WebSocket message can either be 'text' message, a 'binary' message, or a few other control messages.
		// We expect the Bluesky Firehose API to only return binary messages.
		//
		// If we receive a message from the WebSocket that is not binary, then we will just ignore it.
		if websocket.BinaryMessage != wsMessageType {
			continue
		}

		// Here we turn the WebSocket message into a Firehose Message.
		var message firehose.Message = firehose.Message(wsMessage)

		// Here we decode the message.
		var header firehose.MessageHeader
		var payload firehose.MessagePayload = firehose.MessagePayload{}
		err := message.Decode(&header, &paylooad)
		if nil != err {
			fmt.Fprintf(os.Stderr, "ERROR: problem decoding WebSocket message from the connection to the Bluesky Firehose API at %q: %s \n", uri, err)
			continue
		}

		//@TODO: Do whatever you want to do with decode message-header and message-payload..
	}
}

Import

To import package firehose use import code like the follownig:

import "github.com/reiver/go-bsky/firehose"

Installation

To install package firehose do the following:

GOPROXY=direct go get https://github.com/reiver/go-bsky/firehose

Author

DecodePackage firehose was written by Charles Iliya Krempeaux

Documentation

Index

Constants

View Source
const WebSocketURI string = "wss://bsky.network/xrpc/com.atproto.sync.subscribeRepos"

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message []byte

Message represents a message from the Bluesky Firehose websocket.

func (Message) Decode

func (receiver Message) Decode(header *MessageHeader, payload *MessagePayload) error

Decode decodes the message from the Bluesky Firehose websocket into the message-header and the message-payload.

type MessageHeader

type MessageHeader struct {
	Operation Operation `cbor:"op,omitempty"`
	Type      string    `cbor:"t"`
}

The message that comes back from the Bluesky Firehose websocket is 2 CBOR objects concatenated with each other. The first part is called the message-header. The second part is called the message-payload.

MessageHeader represents the message-header.

type MessagePayload

type MessagePayload map[string]any

The message that comes back from the Bluesky Firehose websocket is 2 CBOR objects concatenated with each other. The first part is called the message-header. The second part is called the message-payload.

MessagePayload represents the message-payload.

func (MessagePayload) Blocks

func (receiver MessagePayload) Blocks() (*car.CarReader, error)

func (MessagePayload) Rebase

func (receiver MessagePayload) Rebase() (bool, bool)

func (MessagePayload) Rev

func (receiver MessagePayload) Rev() (string, bool)

func (MessagePayload) Seq

func (receiver MessagePayload) Seq() (int, bool)

func (MessagePayload) Since

func (receiver MessagePayload) Since() (string, bool)

func (MessagePayload) Time

func (receiver MessagePayload) Time() (string, bool)

func (MessagePayload) TooBig

func (receiver MessagePayload) TooBig() (bool, bool)

type Operation

type Operation int64

Operation is the type for the 'Operation' field of MessageHeader.

const (
	OperationRegular Operation = 1
	OperationError   Operation = -1
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL