Skip to content

Commit 594df74

Browse files
committed
Upd with EmitPeers function
1 parent 51aa87b commit 594df74

File tree

7 files changed

+60
-18
lines changed

7 files changed

+60
-18
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [Run(scope, procedure, args) -> interface{}, error](#Run)
2424
* [RunRaw(scope, procedure, args) -> []byte, error](#RunRaw)
2525
* [Emit(scope, roomId, event, args) -> error](#Emit-Conn)
26+
* [EmitPeers(scope, roomId, event, args) -> error](#EmitPeers-Conn)
2627
* [Close()](#Close)
2728
* [Room](#Room)
2829
* [NewRoom(scope, code) -> *Room](#NewRoom)
@@ -34,6 +35,7 @@
3435
* [Join(conn, wait) -> error](#Join)
3536
* [Leave() -> error](#Leave)
3637
* [Emit(event, args) -> error](#Emit)
38+
* [EmitPeers(event, args) -> error](#EmitPeers)
3739

3840
---------------------------------------
3941

@@ -303,6 +305,10 @@ err := conn.Emit(
303305
);
304306
```
305307

308+
### EmitPeers-Conn
309+
310+
Like [Emit](#Emit-Conn), but without echo back. It emits the event to peers only.
311+
306312
### Close
307313

308314
Close an open connection.
@@ -455,3 +461,8 @@ err := room.Emit(
455461
args // Arguments (may be nil)
456462
);
457463
```
464+
465+
### EmitPeers
466+
467+
Like [Emit](#Emit), but without echo back. It emits the event to peers only.
468+

conn.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,31 @@ func (conn *Conn) Emit(scope string, roomId uint64, event string, args []interfa
304304
return err
305305
}
306306

307+
// Emit an even to a room to peers only (no echo back).
308+
//
309+
// If a `Room` is created for the given `roomId`, you probable want to
310+
// use EmitPeers(..) on the `Room` type.
311+
//
312+
// Example:
313+
//
314+
// args := []interface{}{"This is a message"}
315+
//
316+
// err := conn.EmitPeers(
317+
// "//stuff", // scope of the Room
318+
// 123, // Room Id
319+
// "new-message", // Event to emit
320+
// args // Arguments (may be nil)
321+
// );
322+
func (conn *Conn) EmitPeers(scope string, roomId uint64, event string, args []interface{}) error {
323+
data := []interface{}{scope, roomId, event}
324+
if args != nil {
325+
data = append(data, args...)
326+
}
327+
328+
_, err := conn.ensureWrite(ProtoReqEmitPeers, data)
329+
return err
330+
}
331+
307332
// Close an open connection.
308333
//
309334
// > Warning: After calling Close(), the `conn.AutoReconnect` property will be

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/thingsdb/go-thingsdb
22

3-
go 1.15
3+
go 1.19
4+
5+
require github.com/vmihailenco/msgpack/v5 v5.4.1
46

57
require (
68
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/kr/pretty v0.1.0 // indirect
8-
github.com/vmihailenco/msgpack/v5 v5.4.1
9-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
9+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
1010
)

go.sum

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
5-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
6-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
8-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
93
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
124
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
13-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
145
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
156
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
167
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
178
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
18-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
20-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
219
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
22-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

proto.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ const (
5858
ProtoReqLeave Proto = 39
5959
// ProtoReqEmit requires `[scope, roomId, event, arguments...]`
6060
ProtoReqEmit Proto = 40
61+
// ProtoReqEmitPeers requires `[scope, roomId, event, arguments...]`
62+
ProtoReqEmitPeers Proto = 41
6163
)

room.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ func (room *Room) Emit(event string, args []interface{}) error {
211211
return room.conn.Emit(room.scope, room.id, event, args)
212212
}
213213

214+
// Emit an even to a room to peers only (no echo back).
215+
//
216+
// Example:
217+
//
218+
// args := []interface{}{"Just some chat message"}
219+
//
220+
// err := room.EmitPeers(
221+
// "new-message", // Event to emit to peers
222+
// args // Arguments (may be nil)
223+
// );
224+
func (room *Room) EmitPeers(event string, args []interface{}) error {
225+
if room.conn == nil {
226+
return fmt.Errorf("Room Id %d is not joined", room.id)
227+
}
228+
return room.conn.EmitPeers(room.scope, room.id, event, args)
229+
}
230+
214231
func (room *Room) join(conn *Conn) error {
215232
conn.rooms.mux.Lock()
216233
defer conn.rooms.mux.Unlock()

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package thingsdb
22

33
// Version exposes the go-thingsdb version
4-
const Version = "1.0.7"
4+
const Version = "1.0.8"
55

66
//Publish module:
77
//

0 commit comments

Comments
 (0)