realtime

package
v0.0.0-...-fdaaeef Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2016 License: MIT, MIT Imports: 18 Imported by: 0

README

go-realtime

Keep your Go structs in sync with your JS objects

⚠ This project is in beta and the API may change

Features
  • Simple API
  • Works with any JSON marshallable struct
  • Delta updates using JSONPatch
Quick Usage

Server

type Foo struct {
	realtime.Object
	A, B int
}
foo := &Foo{}

//create handler and add foo
rt := realtime.NewHandler()
rt.Add("foo", foo)

//serve websockets and realtime.js client library 
http.Handle("/realtime", rt)

//...later...

//make changes
foo.A = 42
//push to client
foo.Update()

Client

var foo = {};

var rt = realtime("/realtime");

rt.add("foo", foo, function onupdate() {
	//do stuff with foo...
});
Example

See example which is running live here https://go-realtime-demo.herokuapp.com/

Notes
  • Object synchronization is currently one way (server to client) only.
  • Client object properties beginning with $ will be ignored.
MIT License

Copyright © 2015 Jaime Pillora <dev@jpillora.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 JS = jsServe(JSBytes)
View Source
var JSBytes = _realtimeJs

embedded JS file

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type Config

type Config struct {
	Throttle    time.Duration
	CheckOrigin bool
}

type Handler

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

func NewHandler

func NewHandler() *Handler

func NewHandlerConfig

func NewHandlerConfig(c Config) *Handler

func (*Handler) Add

func (r *Handler) Add(k string, v interface{}) error

func (*Handler) MustAdd

func (r *Handler) MustAdd(k string, v interface{})

func (*Handler) ServeHTTP

func (r *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Handler) Update

func (r *Handler) Update(k string)

func (*Handler) UpdateAll

func (r *Handler) UpdateAll()

func (*Handler) UserEvents

func (r *Handler) UserEvents() <-chan *User

type Object

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

an Object is embedded into a parent marshallable struct. an Object has N subscribers. when the parent changes, it is marshalled and sent to each subscriber.

func (*Object) Update

func (o *Object) Update()

Send the changes from this object since the last update Update subscribers

type User

type User struct {
	Connected bool
	ID        string
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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