messages

package module
v0.0.0-...-84a4614 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2014 License: MIT Imports: 8 Imported by: 0

README

##Messages

Messages is a very simple go pkg to send and receive messages using redis as the broker. The message format is simple so other languages can implement the format. A message consists of it's ID, unix timestamp when it was Created, the Mailbox that was used to send it, and the Body which is a json object.

You configure the redis connection via environment variables:

REDIS_PROTO=tcp
REDIS_ADDR=127.0.0.0:6379

###Example

Create a new Mailbox

mbox := messages.NewMailbox("feeds") 

Create a new message with a json encoded body

feed := &Feed{
    Url:    "http://crosbymichael.com/feeds/all.atom.xml",
    Author: "Michael",
}

m := mbox.NewMessage()
if err := m.Marshal(feed); err != nil {
    panic(err)
}

Send the message to the mailbox

if err := mbox.Send(m); err != nil {
    panic(err)
}

Wait for messages to arrive in the mailbox

m, err := mbox.Wait()
if err != nil {
    panic(err)
}

Read data from the message

var newFeed Feed
if err := m.Unmarshal(&newFeed); err != nil {
    panic(err)
}

fmt.Printf("ID: %s\nMailbox: %s\nCreated: %s\n", m.ID, m.Mailbox, m.Time().Format(time.RubyDate))
fmt.Printf("Body: %v\n", newFeed)

Destroy the message data from the mailbox after 500 seconds

if err := mbox.DestoryAfter(m, 500); err != nil {
    panic(err)
}

Result

ID: message:feeds:ec9cf5aee06277cff5dd0ea3e18c79db5d6422e731704e1f24bcfe189d93c7b5
Mailbox: feeds
Created: Sun Sep 15 18:27:25 +0000 2013
Body: {http://crosbymichael.com/feeds/all.atom.xml Michael}

Simple messaging that works across process or the world.

###License MIT

Copyright (c) 2013 Michael Crosby. michael@crosbymichael.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPoolSize = 10 // Number of redis connections to keep in the pool
)

Functions

func NewPool

func NewPool(proto, addr, password string) *redis.Pool

NewPool returns a new redis connection pool

Types

type Mailbox

type Mailbox interface {
	Send(*Message) error
	Wait() (*Message, error)
	DestroyAfter(*Message, int) error
	Close() error
}

func NewMailbox

func NewMailbox(name, proto, addr, password string) Mailbox

Create a new named mailbox to send and receive messages on.

func NewMailboxWithPool

func NewMailboxWithPool(name string, pool *redis.Pool) Mailbox

Create a new named mailbox to send and receive messages on using an existing redis connection pool

type Message

type Message struct {
	ID      string `json:"id"`      // UUID of a message
	Created string `json:"created"` // When the message was created
	Body    []byte `json:"body"`    // Body of the message
}

Message to a recipient

func NewMessage

func NewMessage() *Message

Create a new message

func (*Message) Marshal

func (m *Message) Marshal(v interface{}) error

Marshal a type as the body of the message

func (*Message) Time

func (m *Message) Time() (time.Time, error)

Return a time.Time for the created timestamp

func (*Message) Unmarshal

func (m *Message) Unmarshal(v interface{}) error

Unmarshal the body of the message into a type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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