pubsub

package
v0.1.43 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: GPL-3.0 Imports: 4 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelObject added in v0.1.33

type ChannelObject struct {
	// contains filtered or unexported fields
}

func Channel

func Channel(name string) (*ChannelObject, error)

Channel will create a new ChannelObject based on the provided name returns a ChannelObject and an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/pubsub"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		Channel:      "someChannel",
		WebSocketURL: "ws-QmQq71tkq1yKmYobFUhPWF2MejK5CrvpY4h7HQDDT8b8Zb/someChannel",
	}.Mock()

	channel, err := pubsub.Channel("someChannel")
	if err != nil {
		return
	}

	fmt.Println("Name:", channel.Name())

	err = channel.Subscribe()
	if err != nil {
		return
	}

	fmt.Println("Subscriptions:", m.Subscriptions)

	err = channel.Publish([]byte("Hello, world!"))
	if err != nil {
		return
	}

	fmt.Println("Published:", string(m.PublishedData))

	url, err := channel.WebSocket().Url()
	if err != nil {
		return
	}

	fmt.Println("Url:", url.Path)

}
Output:

Name: someChannel
Subscriptions: [someChannel]
Published: Hello, world!
Url: ws-QmQq71tkq1yKmYobFUhPWF2MejK5CrvpY4h7HQDDT8b8Zb/someChannel

func (*ChannelObject) Name added in v0.1.33

func (c *ChannelObject) Name() string

returns the name of the channel

func (*ChannelObject) Publish added in v0.1.33

func (c *ChannelObject) Publish(data []byte) error

Publish will publish provided data onto the pub-sub channel returns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/pubsub"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		Channel: "someChannel",
	}.Mock()

	channel, err := pubsub.Channel("someChannel")
	if err != nil {
		return
	}

	err = channel.Publish([]byte("Hello, world!"))
	if err != nil {
		return
	}

	fmt.Println("Published:", string(m.PublishedData))
}
Output:

Published: Hello, world!

func (*ChannelObject) Subscribe added in v0.1.33

func (c *ChannelObject) Subscribe() error

Subscribe tells a node to subscribe to a given pub-sub channel returns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/pubsub"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		Channel: "someChannel",
	}.Mock()

	channel, err := pubsub.Channel("someChannel")
	if err != nil {
		return
	}

	err = channel.Subscribe()
	if err != nil {
		return
	}

	fmt.Println("Subs:", m.Subscriptions)
}
Output:

Subs: [someChannel]

func (*ChannelObject) WebSocket added in v0.1.33

func (c *ChannelObject) WebSocket() WebSocket
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/pubsub"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		Channel:      "someChannel",
		WebSocketURL: "ws-QmQq71tkq1yKmYobFUhPWF2MejK5CrvpY4h7HQDDT8b8Zb/someChannel",
	}.Mock()

	channel, err := pubsub.Channel("someChannel")
	if err != nil {
		return
	}

	url, err := channel.WebSocket().Url()
	if err != nil {
		return
	}

	fmt.Println("URL:", url.Path)
}
Output:

URL: ws-QmQq71tkq1yKmYobFUhPWF2MejK5CrvpY4h7HQDDT8b8Zb/someChannel

type PubSubEvent

type PubSubEvent uint32

func (PubSubEvent) Channel

func (e PubSubEvent) Channel() (*ChannelObject, error)

Channel will get the name of the event's channel returns a ChannelObject and an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		Channel: "someChannel",
	}.Mock()

	// An event that would be received by a taubyte function
	var e event.Event

	pubSubEvent, err := e.PubSub()
	if err != nil {
		return
	}

	channel, err := pubSubEvent.Channel()
	if err != nil {
		return
	}

	fmt.Println("Channel:", channel.Name())

}
Output:

Channel: someChannel

func (PubSubEvent) Data

func (e PubSubEvent) Data() ([]byte, error)

Data will get the data received with the pub-sub event returns a byte slice and an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventData: []byte("Hello, world!"),
	}.Mock()

	// An event that would be received by a taubyte function
	var e event.Event

	pubSubEvent, err := e.PubSub()
	if err != nil {
		return
	}

	data, err := pubSubEvent.Data()
	if err != nil {
		return
	}

	fmt.Println("Data:", string(data))

}
Output:

Data: Hello, world!

type PubSubEventMessage

type PubSubEventMessage uint32

type WebSocket added in v0.1.33

type WebSocket ChannelObject

func (WebSocket) Url added in v0.1.33

func (ws WebSocket) Url() (wsUrl url.URL, err error)

WebSocket Url gets a generated url based on the configured channel's ID and the project ID returns a net/url.URL and an error

Jump to

Keyboard shortcuts

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