airtime

package module
v0.0.0-...-2d14f61 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2018 License: MIT Imports: 8 Imported by: 0

README

Airtime

Airtime is a PubSub Go library that uses WebSockets. It includes the server implementation as well as the client.

Once the server is running, the client can connect to a feed by using the Connect("feedname") function. This returns a connection object that allows the client to:

  1. subscribe to any messages sent through the feed
  2. publish meessages to the feed
  3. receive any errors from the feed
  4. close the connection

Installation

go get github.com/hspazio/airtime

Server

package main 

import (
  "log"

  "github.com/hspazio/airtime"
)

func main() {
	svr := airtime.NewServer("8080")
	log.Fatal(svr.Run())
}

Client

package main

import (
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/hspazio/airtime"
)

func main() {
	client := &airtime.Client{Host: "localhost:8080"}

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	conn, err := client.Connect("test")
	if err != nil {
		log.Fatal(err)
	}
	for {
		select {
    // Read messages published to the feed
		case msg := <-conn.Inbox:
			log.Printf("received msg: %s", msg)

    // Publish messages to the feed using the Publish channel
		case <-time.After(1 * time.Second):
			conn.Publish <- []byte("ping")

    // Errors are pushed to the client through a dedicated channel
		case err := <-conn.Errors:
			log.Printf("error: %v", err)

    // Close a connection using the Close() method
		case <-interrupt:
			client.Close()

    // When a connection is fully closed the client is notified 
		case <-conn.Closed:
			log.Println("connection closed")
			return
		}
	}
}

TODO

This is library is still work-in-progress and it's still missing a lot of basic functionalities:

  • authentication
  • make client more resilient to network failures
  • persist and replay undelivered messages when client reconnects
  • read/write permissions management
  • more...

License

Copyright (c) 2018 Fabio Pitino, released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Host string
	// contains filtered or unexported fields
}

Client struct

func (*Client) Close

func (c *Client) Close()

Close the connection currently opened for the client

func (*Client) Connect

func (c *Client) Connect(feed string) (*Connection, error)

Connect to a feed in order to establish a connection

type Connection

type Connection struct {
	Inbox   chan []byte
	Publish chan []byte
	Errors  chan error
	Closed  chan bool
}

Connection handled back to the user

type Hub

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

Hub is the pub-sub server that manages the subscriptions

func NewHub

func NewHub() Hub

NewHub creates a new Hub and spawns it in the background listening for messages to broadcast

func (Hub) Publish

func (h Hub) Publish(feed string, message []byte)

Publish broadcast a message to all the subscribers of the feed

func (Hub) Subscribe

func (h Hub) Subscribe(feed string) *Subscription

Subscribe creates a subscription to a feed using the provided connection

func (Hub) Unsubscribe

func (h Hub) Unsubscribe(feed string, sub *Subscription)

Unsubscribe removes a subscription from the feed

type Server

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

Server for Airtime

func NewServer

func NewServer(port string) *Server

NewServer creates a server

func (*Server) Run

func (s *Server) Run() error

Run the server

type Subscription

type Subscription struct {
	Inbox chan []byte
}

Subscription contains an inbox where the subscriber can pull messages from

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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