├── .gitignore ├── LICENSE ├── README.md ├── emitter.go └── example ├── package.json ├── socket.js ├── socket_client.js └── test.go /.gitignore: -------------------------------------------------------------------------------- 1 | .tern-port 2 | bin/ 3 | vendor/ 4 | node_modules 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 stackcats 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # socket.io-emitter-go 2 | Go implementation of socket.io-emitter 3 | 4 | ## Install 5 | 6 | ```sh 7 | $ go get github.com/stackcats/socket.io-emitter-go 8 | ``` 9 | 10 | ## Example 11 | 12 | ```go 13 | opts := &emitter.Options{} 14 | socket := emitter.NewEmitter(opts) 15 | defer socket.Close() 16 | socket.Broadcast().Emit("message", "Hello World") 17 | ``` 18 | ## License 19 | 20 | MIT 21 | -------------------------------------------------------------------------------- /emitter.go: -------------------------------------------------------------------------------- 1 | package emitter 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "gopkg.in/redis.v5" 7 | "gopkg.in/vmihailenco/msgpack.v2" 8 | ) 9 | 10 | const ( 11 | // https://github.com/socketio/socket.io-parser/blob/master/index.js 12 | gEvent = 2 13 | gBinaryEvent = 5 14 | uid = "emitter" 15 | ) 16 | 17 | // Options ... 18 | type Options struct { 19 | // host to connect to redis on (localhost) 20 | Host string 21 | // port to connect to redis on (6379) 22 | Port int 23 | // password to connect to redis 24 | Password string 25 | // DB 26 | DB int 27 | // the name of the key to pub/sub events on as prefix (socket.io) 28 | Key string 29 | // unix domain socket to connect to redis on ("/tmp/redis.sock") 30 | Socket string 31 | // redis client 32 | Redis *redis.Client 33 | } 34 | 35 | // Emitter Socket.IO redis base emitter 36 | type Emitter struct { 37 | redis *redis.Client 38 | prefix string 39 | rooms []string 40 | flags map[string]interface{} 41 | } 42 | 43 | // NewEmitter Emitter constructor 44 | func NewEmitter(opts *Options) *Emitter { 45 | emitter := &Emitter{} 46 | 47 | if opts.Redis != nil { 48 | emitter.redis = opts.Redis 49 | } else { 50 | host := "127.0.0.1" 51 | if opts.Host != "" { 52 | host = opts.Host 53 | } 54 | 55 | port := 6379 56 | if opts.Port > 0 && opts.Port < 65536 { 57 | port = opts.Port 58 | } 59 | 60 | redisURI := fmt.Sprintf("%s:%d", host, port) 61 | emitter.redis = redis.NewClient(&redis.Options{ 62 | Addr: redisURI, 63 | Password: opts.Password, 64 | DB: opts.DB, 65 | }) 66 | } 67 | 68 | emitter.prefix = "socket.io" 69 | if opts.Key != "" { 70 | emitter.prefix = opts.Key 71 | } 72 | 73 | emitter.rooms = make([]string, 0, 0) 74 | 75 | emitter.flags = make(map[string]interface{}) 76 | 77 | return emitter 78 | } 79 | 80 | // Close release redis client 81 | func (e *Emitter) Close() { 82 | if e.redis != nil { 83 | e.redis.Close() 84 | } 85 | } 86 | 87 | // Emit Send the packet 88 | func (e *Emitter) Emit(data ...interface{}) (*Emitter, error) { 89 | packet := make(map[string]interface{}) 90 | packet["type"] = gEvent 91 | if hasBin(data...) { 92 | packet["type"] = gBinaryEvent 93 | } 94 | 95 | packet["data"] = data 96 | 97 | packet["nsp"] = "/" 98 | if nsp, ok := e.flags["nsp"]; ok { 99 | packet["nsp"] = nsp 100 | delete(e.flags, "nsp") 101 | } 102 | 103 | opts := map[string]interface{}{ 104 | "rooms": e.rooms, 105 | "flags": e.flags, 106 | } 107 | 108 | chn := fmt.Sprintf("%s#%s#", e.prefix, packet["nsp"]) 109 | 110 | buf, err := msgpack.Marshal([]interface{}{uid, packet, opts}) 111 | if err != nil { 112 | return nil, err 113 | } 114 | 115 | if len(e.rooms) > 0 { 116 | for _, room := range e.rooms { 117 | chnRoom := fmt.Sprintf("%s%s#", chn, room) 118 | e.redis.Publish(chnRoom, string(buf)) 119 | } 120 | } else { 121 | e.redis.Publish(chn, string(buf)) 122 | } 123 | 124 | e.rooms = make([]string, 0, 0) 125 | e.flags = make(map[string]interface{}) 126 | return e, nil 127 | } 128 | 129 | // In Limit emission to a certain `room` 130 | func (e *Emitter) In(room string) *Emitter { 131 | for _, r := range e.rooms { 132 | if r == room { 133 | return e 134 | } 135 | } 136 | e.rooms = append(e.rooms, room) 137 | return e 138 | } 139 | 140 | // To Limit emission to a certain `room` 141 | func (e *Emitter) To(room string) *Emitter { 142 | return e.In(room) 143 | } 144 | 145 | // Of Limit emission to certain `namespace` 146 | func (e *Emitter) Of(namespace string) *Emitter { 147 | e.flags["nsp"] = namespace 148 | return e 149 | } 150 | 151 | // JSON flag 152 | func (e *Emitter) JSON() *Emitter { 153 | e.flags["json"] = true 154 | return e 155 | } 156 | 157 | // Volatile flag 158 | func (e *Emitter) Volatile() *Emitter { 159 | e.flags["volatile"] = true 160 | return e 161 | } 162 | 163 | // Broadcast flag 164 | func (e *Emitter) Broadcast() *Emitter { 165 | e.flags["broadcast"] = true 166 | return e 167 | } 168 | 169 | func hasBin(data ...interface{}) bool { 170 | if data == nil { 171 | return false 172 | } 173 | 174 | for _, d := range data { 175 | switch res := d.(type) { 176 | case []byte: 177 | return true 178 | case bytes.Buffer: 179 | return true 180 | case []interface{}: 181 | for _, each := range res { 182 | if hasBin(each) { 183 | return true 184 | } 185 | } 186 | case map[string]interface{}: 187 | for _, val := range res { 188 | if hasBin(val) { 189 | return true 190 | } 191 | } 192 | default: 193 | return false 194 | } 195 | } 196 | 197 | return false 198 | } 199 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socket.io-emitter-go-test", 3 | "version": "1.0.0", 4 | "description": "socke.io-emitter-go-test", 5 | "scripts": {}, 6 | "author": "stackcats", 7 | "license": "MIT", 8 | "dependencies": { 9 | "redis": "^2.5.3", 10 | "socket.io": "^1.4.6", 11 | "socket.io-client": "^1.7.2", 12 | "socket.io-redis": "3.1.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /example/socket.js: -------------------------------------------------------------------------------- 1 | const adapter = require('socket.io-redis'); 2 | const redis = require('redis'); 3 | const io = require('socket.io')(3000); 4 | 5 | const pub = redis.createClient(); 6 | const sub = redis.createClient(); 7 | 8 | io.adapter(adapter({ 9 | pubClient: pub, 10 | subClient: sub, 11 | key: 'socket.io' 12 | })); 13 | 14 | const nsp = io.of('stackcats'); 15 | 16 | nsp.on('connection', (socket) => { 17 | socket.emit('helloclient', 'test'); 18 | socket.join('test1'); 19 | socket.join('test2'); 20 | socket.on('helloserver', (msg) => { 21 | console.log(msg); 22 | }); 23 | }); 24 | 25 | nsp.on('error', (err) => { 26 | console.log(err); 27 | }); 28 | -------------------------------------------------------------------------------- /example/socket_client.js: -------------------------------------------------------------------------------- 1 | const socket = require('socket.io-client')('http://localhost:3000/stackcats'); 2 | 3 | socket.on('connect', () => { 4 | console.log('connect'); 5 | }); 6 | 7 | socket.on('helloclient', (msg) => { 8 | console.log('helloclient: ', msg); 9 | socket.emit('helloserver', 'Hello World'); 10 | }); 11 | 12 | socket.on('disconnect', () => { 13 | console.log('disconnect'); 14 | }); 15 | 16 | socket.on('error', (err) => { 17 | console.log(err); 18 | }); 19 | -------------------------------------------------------------------------------- /example/test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/stackcats/socket.io-emitter-go" 5 | ) 6 | 7 | func main() { 8 | opts := &emitter.Options{} 9 | socket := emitter.NewEmitter(opts) 10 | defer socket.Close() 11 | socket.In("test1").Of("/stackcats").Emit("helloclient", map[string]interface{}{ 12 | "action": 1, 13 | "name": "heheeh", 14 | }) 15 | } 16 | --------------------------------------------------------------------------------