Skip to content

Synternet/pubsub-go

Repository files navigation

Syntropy PubSub Go

Welcome to the Golang SDK documentation for the Data Layer. This SDK enables seamless integration with our Data Layer solution, allowing you to harness the power of real-time data streams in your Golang applications. By leveraging the capabilities of the Data Layer, you can unlock new possibilities for data-driven projects and applications.

syntropy-pubsub-go is a Golang library designed specifically for the Syntropy Data Layer project. With this library, you can effortlessly subscribe to existing data streams or publish new ones. Powered by the NATS messaging system, [syntropy-pubsub-go](https://github.com/SyntropyNet/pubsub-go) offers seamless integration between your Golang applications and the Syntropy Data Layer platform.

Features

  • Subscribe to Existing Data Streams: Easily subscribe to pre-existing data streams within the Syntropy Data Layer. Receive real-time updates and harness the power of real-time data insights in your Golang applications.

  • Publish New Data Streams: Create and publish your own data streams directly from your Golang applications. Share data with other participants in the Data Layer, facilitating collaboration and enabling the creation of innovative data-driven solutions.

  • Support for JSON Messages: Leverage the flexibility and interoperability of JSON messages. syntropy-pubsub-go provides support for handling JSON data, making it easy to work with complex data structures and seamlessly integrate with other systems and platforms.

  • Customizable Connection Options: Tailor the connection options to suit your specific needs. Configure parameters such as connection timeouts, retry mechanisms, and authentication details to ensure a secure and reliable connection to the Syntropy Data Layer platform.

By utilizing the syntropy-pubsub-go library, you can unlock the full potential of the Syntropy Data Layer in your Golang applications. Seamlessly integrate with existing data streams, create new ones, and harness the power of real-time data insights to drive innovation and solve complex problems.

Note: Customize the feature list and add more details as necessary based on the specific capabilities and functionalities of your Golang SDK for the Syntropy Data Layer.

Installation

To install the Golang SDK for Data Layer, you can use Go modules or include it as a dependency in your project. Here's an example of how to add it to your project:

go get github.com/SyntropyNet/pubsub-go

Make sure to import the package in your code:

import "github.com/SyntropyNet/pubsub-go"

Getting Started

Before you start using the Golang SDK, make sure you have the necessary credentials and access tokens from the Syntropy Developer Portalplatform. These credentials will allow you to connect to the Data Availability Layer and subscribe to the desired data streams.

Examples

For detailed usage examples, please refer to the examples directory in the repository. These examples cover various scenarios and demonstrate how to utilize the SDK's features effectively.

Subscribe

package main

import (
	"context"
	"github.com/SyntropyNet/pubsub-go/pubsub"
	"github.com/nats-io/nats.go"
	"log"
	"os"
	"os/signal"
	"syscall"
)

const (
	natsUrl                 = "nats://127.0.0.1"
	accessToken             = "EXAMPLE_ACCESS_TOKEN"
	exampleSubscribeSubject = "example.sub.subject"
)

// RepublishData receives a message on a given subject and republishes it to another subject.
// It takes a context, the service instance, and the data (message) as input arguments.
func PrintData(ctx context.Context, service *pubsub.NatsService, data []byte) error {
	log.Println("Received message on", exampleSubscribeSubject, "subject")
	return nil
}

func main() {
	jwt, _ := pubsub.CreateAppJwt(accessToken)
	println(jwt)
	// Set user credentials and options for NATS connection.
	opts := []nats.Option{}
	opts = append(opts, nats.UserJWTAndSeed(jwt, accessToken))

	// Connect to the NATS server using the provided options.
	service := pubsub.MustConnect(
		pubsub.Config{
			URI:  natsUrl,
			Opts: opts,
		})
	log.Println("Connected to NATS server.")

	// Create a context with a cancel function to control the cancellation of ongoing operations.
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Add a handler function to process messages received on the exampleSubscribeSubject.
	service.AddHandler(exampleSubscribeSubject, func(data []byte) error {
		return PrintData(ctx, service, data)
	})

	// Set up signal handling to gracefully shut down when receiving SIGINT or SIGTERM signals.
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-signalChan
		cancel()
	}()

	// Start serving messages and processing them using the registered handler function.
	service.Serve(ctx)
}

Publish

package main

import (
	"context"
	"github.com/SyntropyNet/pubsub-go/pubsub"
	"github.com/nats-io/nats.go"
	"log"
	"os"
	"os/signal"
	"syscall"
)

const (
	natsUrl                 = "nats://127.0.0.1:4222"
	userCredsJWT            = "USER_JWT"
	accessToken             = "EXAMPLE_ACCESS_TOKEN"
	exampleSubscribeSubject = "example.sub.subject"
	examplePublishSubject   = "example.pub.subject"
)

// RepublishData receives a message on a given subject and republishes it to another subject.
// It takes a context, the service instance, and the data (message) as input arguments.
func RepublishData(ctx context.Context, service *pubsub.NatsService, data []byte) error {
	log.Println("Received message on", exampleSubscribeSubject, "subject")
	err := service.Publish(ctx, examplePublishSubject, data)
	if err != nil {
		return err
	}
	log.Println("Published message on", examplePublishSubject, "subject")
	return nil
}

func main() {
	// Set user credentials and options for NATS connection.
	opts := []nats.Option{}
	opts = append(opts, nats.UserJWTAndSeed(userCredsJWT, accessToken))

	// Connect to the NATS server using the provided options.
	service := pubsub.MustConnect(
		pubsub.Config{
			URI:  natsUrl,
			Opts: opts,
		})
	log.Println("Connected to NATS server.")

	// Create a context with a cancel function to control the cancellation of ongoing operations.
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Add a handler function to process messages received on the exampleSubscribeSubject.
	service.AddHandler(exampleSubscribeSubject, func(data []byte) error {
		return RepublishData(ctx, service, data)
	})

	// Set up signal handling to gracefully shut down when receiving SIGINT or SIGTERM signals.
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-signalChan
		cancel()
	}()

	// Start serving messages and processing them using the registered handler function.
	service.Serve(ctx)
}

Contributing

We welcome contributions from the community! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository. We appreciate your feedback and collaboration in making this SDK even better.

Contribution Guidelines

To contribute to this project, please follow the guidelines outlined in the Contribution.md file. It covers important information about how to submit bug reports, suggest new features, and submit pull requests.

Code of Conduct

This project adheres to a Code of Conduct to ensure a welcoming and inclusive environment for all contributors. Please review the guidelines and make sure to follow them in all interactions within the project.

Commit Message Format

When making changes to the codebase, it's important to follow a consistent commit message format. Please refer to the Commit Message Format for details on how to structure your commit messages.

Pull Request Template

To streamline the pull request process, we have provided a Pull Request Template that includes the necessary sections for describing your changes, related issues, proposed changes, and any additional information. Make sure to fill out the template when submitting a pull request.

We appreciate your contributions and thank you for your support in making this project better!

Support

If you encounter any difficulties or have questions regarding the Golang SDK for Data Layer, please reach out to our support team at Discord #developer-discussion. We are here to assist you and ensure a smooth experience with our SDK.

We hope this documentation provides you with a comprehensive understanding of the Golang SDK for the Data Layer. Happy coding with real-time data streams and enjoy the power of the Data Layer in your Golang applications!

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages