gowmb

package module
v0.0.0-...-8abe755 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2016 License: Apache-2.0 Imports: 6 Imported by: 0

README

Coverage Status Build Status

gowmb

A (very) simple message broker over websockets in go

Package gowmb (go Websocket Message Broker) manages message processing and broadcast between various clients, based on a segregation "tag".

Usage

The package is go-gettable

go get -v github.com/owulveryck/gowmb

See GoDoc reference for examples and more.

Credits

This package is heavily inspired by the "chat example" of the gorilla's websocket implementation package.

Documentation

Overview

Package gowmb (go Websocket Message Broker) manages message processing and broadcast between various clients, based on a segregation "tag".

Basics

The CreateHandler should be used to create a http.Handler type that can be used with the net/http (or any good http's muxer package).

Assuming you've defined a Messenger'c compatible message type, every client connected to the web-path server by the defined handler will receive a Serialize version of the Message each time the server's websocket detects an event.

Tip's and tricks

The broker does not validate the format of the message. The message is "acked" at the protocol level, but the broker does not advertize any client that a message has been discarded

Example
package main

import (
	"encoding/json"
	"github.com/gorilla/mux"
	"github.com/owulveryck/gowmb"
	"log"
	"net/http"
	"strconv"
)

// message is the top envelop for message communication between nodes
type message struct {
	ID int `json:"id"`
}

// Createmessage creates a new message and returns a pointer
func createMessage() *message {
	return &message{}
}

// Serialize returns a byte array of the message
func (m *message) Serialize() ([]byte, error) {
	return json.Marshal(m)
}

// Set function updates the content of message m awwording to input n
// And it fills the Msg's interface Contract
func (m *message) Set(n []byte) error {
	type input struct {
		ID int `json:"int"`
	}
	var message input
	err := json.Unmarshal(n, &message)
	if err != nil {
		return err
	}
	m.ID = message.ID
	return nil
}

type tag int

func (t *tag) Parse(s string) error {
	v, err := strconv.Atoi(s)
	if err != nil {
		return err
	}
	*t = tag(v)
	return nil
}

func newTag() *tag {
	return new(tag)
}

func main() {
	router := mux.NewRouter().StrictSlash(true)

	handler := gowmb.CreateHandler(createMessage(), newTag(), "tag")
	router.
		Methods("GET").
		Path("/serveWs/{tag}").
		Name("WebSocket").
		HandlerFunc(handler)
	log.Fatal(http.ListenAndServe(":8080", router))

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHandler

func CreateHandler(creator Messager, tag Tag, tagName string) func(http.ResponseWriter, *http.Request)

CreateHandler returns a http.handler. It takes as input a Messager interface

Types

type Messager

type Messager interface {
	Set(n []byte) error
	Serialize() ([]byte, error)
}

Messager is an interface. The message can be setted via the set method. The message is encoded by the Serialize method and sent through the sockets

type Tag

type Tag interface {
	Parse(s string) error
}

The Tag for channel separation

Jump to

Keyboard shortcuts

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