-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.go
More file actions
107 lines (89 loc) · 2.84 KB
/
chat.go
File metadata and controls
107 lines (89 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/ably/ably-go/ably"
"github.com/ably/ably-go/ably/proto"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
var username string
// If no username specified, ask for one
if len(os.Args) < 2 {
fmt.Println("Type your username")
reader := bufio.NewReader(os.Stdin)
username, _ = reader.ReadString('\n')
username = strings.Replace(username, "\n", "", -1)
} else {
username = os.Args[1]
}
// Connect to Ably using the API key and ClientID specified above
client, err := ably.NewRealtime(
ably.WithKey(os.Getenv("ABLY_KEY")),
// ably.WithEchoMessages(true), // Uncomment to stop messages you send from being sent back
ably.WithClientID(username))
if err != nil {
panic(err)
}
fmt.Println("You can now send messages!")
// Connect to the Ably Channel with name 'chat'
channel := client.Channels.Get("chat")
// Enter the Presence set of the channel
channel.Presence.Enter(context.Background(), "")
getHistory(channel)
subscribe(channel)
// Start the goroutine to allow for publishing messages
publishing(channel)
}
func getHistory(channel *ably.RealtimeChannel) {
// Before subscribing for messages, check the channel's
// History for any missed messages. By default a channel
// will keep 2 minutes of history available, but this can
// be extended to 48 hours
page, err := channel.History(nil)
for ; err == nil && page != nil; page, err = page.Next() {
for _, msg := range page.Messages() {
fmt.Printf("Previous message from %v: '%v'\n", msg.ClientID, msg.Data)
}
}
}
func subscribe(channel *ably.RealtimeChannel) {
// Subscribe to messages sent on the channel
_, err := channel.SubscribeAll(context.Background(), func(msg *ably.Message) {
fmt.Printf("Received message from %v: '%v'\n", msg.ClientID, msg.Data)
})
if err != nil {
err := fmt.Errorf("subscribing to channel: %w", err)
fmt.Println(err)
}
// Subscribe to presence events (people entering and leaving) on the channel
_, pErr := channel.Presence.SubscribeAll(context.Background(), func(msg *ably.PresenceMessage) {
if msg.Action == proto.PresenceEnter {
fmt.Printf("%v has entered the chat\n", msg.ClientID)
} else if msg.Action == proto.PresenceLeave {
fmt.Printf("%v has left the chat\n", msg.ClientID)
}
})
if pErr != nil {
err := fmt.Errorf("subscribing to presence in channel: %w", pErr)
fmt.Println(err)
}
}
func publishing(channel *ably.RealtimeChannel) {
reader := bufio.NewReader(os.Stdin)
for {
text, _ := reader.ReadString('\n')
text = strings.ReplaceAll(text, "\n", "")
// Publish the message typed in to the Ably Channel
err := channel.Publish(context.Background(), "message", text)
// await confirmation that message was received by Ably
if err != nil {
err := fmt.Errorf("publishing to channel: %w", err)
fmt.Println(err)
}
}
}