Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ TODO
build
mist
vendor/*/
.vscode
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: go
go:
- "1.10"
- "1.12"

env:
- GO111MODULE=on

addons:
postgresql: "9.3"
Expand All @@ -11,13 +14,10 @@ services:
before_script:
- sudo -H pip install awscli

install:
- go get github.com/mitchellh/gox
- go get github.com/kardianos/govendor
- govendor sync
install: true

script:
- govendor test +local -cover -v
- go test -v ./...

after_success:
- export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo $TRAVIS_PULL_REQUEST_BRANCH; fi)
Expand Down
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.12

# Prepare external Go packages:
# we're going to add go.mod and go.sum into /tmp and download all of our go dependencies.
ENV GOPATH=/go
ENV GO111MODULE=on
ADD go.mod go.sum /tmp/go/
WORKDIR /tmp/go
RUN go mod download
# reset WORKDIR and remove tmp files
WORKDIR /
RUN rm -rf /tmp/go

ADD . /go/src/github.com/nanopack/mist
WORKDIR /go/src/github.com/nanopack/mist
RUN CGO_ENABLED=1 go build -o /mist main.go

# We don't want to run as root in production. Create a "mist" user.
RUN addgroup mist && useradd -g mist mist

# Tell docker that all future commands should run as the appuser user
USER mist

ENTRYPOINT [ "/mist" ]
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DISPLAY_BOLD := "\033[01m"
DISPLAY_RESET := "\033[0;0m"

VERSION ?= 1.3

# Where we're running Make from
SRC_DIR ?= $(shell pwd)

# We'll publish all containers out of this repo into deepmap-go scope.
DOCKER_IMAGE_NAME := mist

# This rule builds our Go development environment in a Docker container
.phony: docker

docker:
docker build -t $(DOCKER_IMAGE_NAME):$(VERSION) .

docker-push:
docker push $(DOCKER_IMAGE_NAME):$(VERSION)
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ Each Message has a set of `tags` and `data`. Tags can take any form you like, as

### Subscribing / Publishing

Think of `tags` as a way to filter out messages you don't want to receive; the more tags that are added to a subscription the more direct a message has to be:
Think of `tags` as a way to filter out messages you don't want to receive, which means it will only receive messages
contain all of the subscriped tags; if a client has multiple subscribe message, it will receive the message if any of
the subscription satisfy the requirement. the more tags that are added to a subscription the more direct a message has to be:

| Subscribed tags | Messages received from tags |
| --- | --- |
| `["onefish"]` | `["onefish"]`, `["onefish","twofish"]`, `["onefish","twofish","redfish"]` |
| `["onefish", "twofish"]` | `["onefish","twofish"]`, `["onefish","twofish","redfish"]` |
| `["onefish", "twofish", "redfish"]` | `["onefish","twofish","redfish"]` |
| `["twofish"]` | `["onefile", "twofish"]` |

Message that are published to clients as the result of a subscription are delivered in this format:

Expand Down Expand Up @@ -236,7 +239,7 @@ prior to chrome console letting you run the following javascript.
``` javascript

// connect the websocket
var ws = new WebSocket("ws://localhost:8080/subscribe/websocket?x-auth-token=token")
var ws = new WebSocket("ws://localhost:8080/ws?x-auth-token=token")

// handle responses from the server
ws.onmessage = function(me){
Expand Down
17 changes: 3 additions & 14 deletions clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"io"
"net"
"time"

"github.com/jcelliott/lumber"

"github.com/nanopack/mist/core"
mist "github.com/nanopack/mist/core"
)

type (
Expand Down Expand Up @@ -141,28 +140,18 @@ func (c *TCP) Publish(tags []string, data string) error {
return c.encoder.Encode(&mist.Message{Command: "publish", Tags: tags, Data: data})
}

// PublishAfter sends a message to the mist server to be published to all subscribed
// clients after a specified delay
func (c *TCP) PublishAfter(tags []string, data string, delay time.Duration) error {
go func() {
<-time.After(delay)
c.Publish(tags, data)
}()
return nil
}

// List requests a list from the server of the tags this client is subscribed to
func (c *TCP) List() error {
return c.encoder.Encode(&mist.Message{Command: "list"})
}

// listall related
// ListAll related
// List requests a list from the server of the tags this client is subscribed to
func (c *TCP) ListAll() error {
return c.encoder.Encode(&mist.Message{Command: "listall"})
}

// who related
// Who related
// Who requests connection/subscriber stats from the server
func (c *TCP) Who() error {
return c.encoder.Encode(&mist.Message{Command: "who"})
Expand Down
7 changes: 0 additions & 7 deletions clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clients_test
import (
"os"
"testing"
"time"

"github.com/jcelliott/lumber"

Expand Down Expand Up @@ -107,12 +106,6 @@ func TestTCPClient(t *testing.T) {
t.Fatalf("publishing no data succeeded %s", err.Error())
}

// test PublishAfter
if err := client.PublishAfter([]string{"a"}, "testpublish", time.Second); err != nil {
t.Fatalf("publishing failed %s", err.Error())
}
time.Sleep(time.Millisecond * 1500)

// test ability to unsubscribe
if err := client.Unsubscribe([]string{"a"}); err != nil {
t.Fatalf("client unsubscriptions failed %s", err.Error())
Expand Down
19 changes: 2 additions & 17 deletions core/mist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/jcelliott/lumber"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func Subscribers() string {

// slice it
subSlice := []string{}
for k, _ := range subs {
for k := range subs {
subSlice = append(subSlice, k)
}

Expand All @@ -68,28 +67,14 @@ func Who() (int, int) {

// todo: delete these 2. limiting what is a subscriber makes this not needed
// if they subscribe to a thing on a reused connection, they wanted to get updates.. hopefully
//

// Publish publishes to ALL subscribers. Usefull in client applications
// who reuse the publish connection for subscribing (publishes to self)
func Publish(tags []string, data string) error {
lumber.Trace("Publishing...")
return publish(0, tags, data)
}

// PublishAfter publishes to ALL subscribers. Usefull in client applications
// who reuse the publish connection for subscribing
func PublishAfter(tags []string, data string, delay time.Duration) error {
go func() {
<-time.After(delay)
if err := Publish(tags, data); err != nil {
// log this error and continue?
lumber.Error("Failed to PublishAfter - %s", err.Error())
}
}()

return nil
}

// publish publishes to all subscribers except the one who issued the publish
func publish(pid uint32, tags []string, data string) error {

Expand Down
12 changes: 0 additions & 12 deletions core/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mist
import (
"sync"
"sync/atomic"
"time"

"github.com/jcelliott/lumber"
)
Expand Down Expand Up @@ -119,17 +118,6 @@ func (p *Proxy) Publish(tags []string, data string) error {
return publish(p.id, tags, data)
}

// PublishAfter sends a message after [delay]
func (p *Proxy) PublishAfter(tags []string, data string, delay time.Duration) {
go func() {
<-time.After(delay)
if err := publish(p.id, tags, data); err != nil {
// log this error and continue
lumber.Error("Proxy failed to PublishAfter - %s", err.Error())
}
}()
}

// List returns a list of all current subscriptions
func (p *Proxy) List() (data [][]string) {
lumber.Trace("Proxy listing subscriptions...")
Expand Down
22 changes: 22 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/nanopack/mist

go 1.12

require (
github.com/deckarep/golang-set v1.7.1
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.4.0
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25
github.com/lib/pq v1.1.1
github.com/magiconair/properties v1.8.1 // indirect
github.com/nanobox-io/golang-nanoauth v0.0.0-20190311151057-c2ebbac481bb
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
golang.org/x/text v0.3.2 // indirect
)
Loading