Code Monkey home page Code Monkey logo

go-simplesub's Introduction

go-simplesub

A minimalistic, yet powerful pubsub messaging system built on top of libp2p.

Rationale

Why does this repo exist?

Simple: Libp2p's pub/sub implementation simply does not provide enough flexibility for some use cases (those that utilize a DHT or any sort of routing in particular).

Installation

go get github.com/dowlandaiello/go-simplesub

Usage

package main

import (
    "context"

    "github.com/libp2p/go-libp2p"
    dht "github.com/libp2p/go-libp2p-kad-dht"
    routed "github.com/libp2p/go-libp2p/p2p/host/routed"
    "github.com/dowlandaiello/go-simplesub"
    inet "github.com/libp2p/go-libp2p-net"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background()) // Initialize context

    defer cancel() // Cancel

    host, err := libp2p.New(
        ctx,
        libp2p.NATPortMap(),
        libp2p.ListenAddrStrings(
            "/ip4/0.0.0.0/tcp/1111",
            "/ip6/::1/tcp/1111",
        ),
    ) // Initialize host

    if err != nil { // Check for errors
        panic(err) // Panic
    }

    dht, err := dht.New(ctx, host) // Initialize dht

    if err != nil { // Check for errors
        panic(err) // Panic
    }

    routedHost := routed.Wrap(host, dht) // Wrap host

    sub, err := simplesub.NewSimpleSub(routedHost) // Initialize sub

    if err != nil { // Check for errors
        panic(err) // Panic
    }

    sub.Subscribe("test_topic", handler) // Subscribe

    err = sub.Publish(ctx, "test_topic", []byte("test")) // Publish to topic

    if err != nil { // Check for errors
        panic(err) // Panic
    }
}

// handler handles a new incoming stream.
func handler(stream inet.Stream, message *simplesub.Message) {
    fmt.Printf("Received message: %s", string(message.Data)) // Log received
}

Configuration

Route Prefixes

By default, all simplesub routes are registered under /. Should one wish to add a prefix to such a route, simply pass the WithRoutePrefix option function to the simplesub constructor.

Example:

sub, err := simplesub.NewSimpleSub(routedHost, simplesub.WithRoutePrefix("test_net")) // Initialize sub

if err != nil { // Check for errors
    panic(err) // Panic
}

go-simplesub's People

Contributors

dowlandaiello avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.