spaghetti

package module
v0.0.0-...-90eeaa2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2014 License: MIT Imports: 8 Imported by: 0

README

Spaghetti

A simple router (using Web-Sockets transport) to implement Pub/Sub messages delivery. This is not a real PubSub, not a Message Broker, not a Queue. There are no topics, exchanges, etc. This is a simple tool that helps to bind a bunch of devices and services in the office so they could communicate easily without too much network programming overhead.

Installation

go get github.com/oleksandr/spaghetti
go install github.com/oleksandr/spaghetti/cmd/spaghetti

Usage

Running the router:

$ ./bin/spaghetti -bind=0.0.0.0 -port=3000

Connecting to another spaghetti router:

$ ./bin/spaghetti -bind=0.0.0.0 -port=3100 -uplink=ws://localhost:3000/ws/pubsub

Available endpoints

There are 3 endpoints for connecting to the router (either directly or using uplink):

  • Pub (ws://localhost:3000/ws/pub)
  • Sub (ws://localhost:3000/ws/sub)
  • PubSub (ws://localhost:3000/ws/pubsub)

As you can guess, Pub mode will allow the connected client/router only to send message to the network. Sub mode will only wait for message delivered from router. PubSub is for a bi-directional communication.

Giving it a try

If you don't have Web-socket client or too lazy to program it just use the following URL http://www.websocket.org/echo.html with echo client implemented to connect to your router.

An example of a topology you can build with spaghetti

Example

Documentation

Index

Constants

View Source
const (
	ConnectionTypePub    = "Pub"
	ConnectionTypeSub    = "Sub"
	ConnectionTypePubSub = "PubSub"
)

Variables

View Source
var DefaultHub = Hub{

	Register:   make(chan *Connection),
	Unregister: make(chan *Connection),
	Data:       make(chan *Message),
	// contains filtered or unexported fields
}

The default Hub instance used all around this code

Functions

func PubSubWebHandler

func PubSubWebHandler(ws *websocket.Conn)

Handle connection from clients for both publications and subscriptions

func PubWebHandler

func PubWebHandler(ws *websocket.Conn)

Handle connection from clients for publications only

func SubWebHandler

func SubWebHandler(ws *websocket.Conn)

Handle connection from clients for subscriptions only

Types

type Connection

type Connection struct {
	ID      string
	WS      *websocket.Conn
	Type    ConnectionType
	Send    chan *Message
	Created time.Time
	Updated time.Time
}

Connection data structure

func NewConnection

func NewConnection(ws *websocket.Conn) *Connection

Connection constructor

func (*Connection) Close

func (c *Connection) Close()

Closes the connection

func (*Connection) Reader

func (c *Connection) Reader()

Reads from connection

func (*Connection) Writer

func (c *Connection) Writer()

Write to connection

type ConnectionType

type ConnectionType string

type Hub

type Hub struct {
	// Unique name for this hub
	ID string

	// Register requests from the connections
	Register chan *Connection

	// Unregister requests from connections
	Unregister chan *Connection

	// Uplink connection settings
	Uplink     *Connection
	UplinkType ConnectionType

	// Data handling channel
	Data chan *Message
	// contains filtered or unexported fields
}

The communication hub between the active connections

func (*Hub) RouteMessage

func (self *Hub) RouteMessage(message *Message)

Route a message to subscribers

func (self *Hub) SetupUplink(uplinkUrl string)

Establish uplink connection toa given WS url

func (*Hub) Start

func (self *Hub) Start()

Start the hub and listen for incoming packets

type Message

type Message struct {
	ConnId string `json:"-"`
	Body   interface{}
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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