Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5985b44

Browse files
committed
Finished tutorial.
1 parent 04755d6 commit 5985b44

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Go-Chat
22
Console chat utility that demonstrates [PubNub](https://www.pubnub.com/) integration with Golang.
33

4+
![Image of Go-Chat](https://raw.githubusercontent.com/chandler767/Go-Chat/master/images/chat.png)
5+
46
## Features
57
- Leverages the PubNub Network for chat with [Publish and Subscribe](https://www.pubnub.com/docs/go/data-streams-publish-and-subscribe).
68
- Uses [GoCUI](https://github.com/jroimartin/gocui) for a simple console user interface.
@@ -76,11 +78,11 @@ func drawchat(channel string, username string) {
7678
ov.Wrap = true
7779

7880
// Send a welcome message.
79-
_, err = fmt.Fprintln(ov, "<App>: Welcome to Go-Chat powered by PubNub!")
81+
_, err = fmt.Fprintln(ov, "<Go-Chat>: Welcome to Go-Chat powered by PubNub!")
8082
if err != nil {
8183
log.Println("Failed to print into output view:", err)
8284
}
83-
_, err = fmt.Fprintln(ov, "<App>: Press Ctrl-C to quit.")
85+
_, err = fmt.Fprintln(ov, "<Go-Chat>: Press Ctrl-C to quit.")
8486
if err != nil {
8587
log.Println("Failed to print into output view:", err)
8688
}
@@ -151,6 +153,7 @@ func drawchat(channel string, username string) {
151153
}
152154

153155
func main() {
156+
// Get channel and username.
154157
reader := bufio.NewReader(os.Stdin)
155158
fmt.Print("Enter Channel Name: ")
156159
channel, err := reader.ReadString('\n')
@@ -162,15 +165,17 @@ func main() {
162165
if err != nil {
163166
log.Println("Could not set username:", err)
164167
}
168+
// Create the GUI.
165169
drawchat(strings.TrimSuffix(channel, "\n"), strings.TrimSuffix(username, "\n"))
166170
}
167171

168172
```
169173
5. Run the application: `go run main.go`. You should be able to enter a channel name, username, and send messages to yourself.
174+
170175
![Image of application GUI](https://raw.githubusercontent.com/chandler767/Go-Chat/master/images/UI.png)
171176

172177
6. Now we need to integrate PubNub to send messages to other users and receive messages.
173-
7. You need PubNub API Keys. This allows the chat communication over a data stream network. You can fill in the YOUR_PUBLISH_API_KEY and YOUR_SUBSCRIBE_API_KEY placeholder strings with your API keys that you get on the (PubNub website)[http://pubnub.com/].
178+
7. You need PubNub API Keys. This allows the chat communication over a data stream network. You can fill in the YOUR_PUBLISH_API_KEY and YOUR_SUBSCRIBE_API_KEY placeholder strings with your API keys that you get on the [PubNub website](http://pubnub.com/).
174179
8. Import the PubNub messaging package and the encoding/json package.
175180
```
176181
"github.com/pubnub/go/messaging"
@@ -218,6 +223,44 @@ go func() {
218223
}
219224
}()
220225
```
226+
11. Change your enter key binding function to send messages to PubNub instead of displaying them in the output. The messages will display in the output if they are succesfully sent to PubNub and received by pubnub.Subscribe() function.
227+
```go
228+
// Bind enter key to input to send new messages.
229+
err = g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, func(g *gocui.Gui, iv *gocui.View) error {
230+
// Read buffer from the beginning.
231+
iv.Rewind()
232+
233+
// Send message if text was entered.
234+
if len(iv.Buffer()) >= 2 {
235+
go pubnub.Publish(
236+
channel,
237+
"<"+username+">: "+iv.Buffer(),
238+
make(chan []byte),
239+
make(chan []byte),
240+
)
241+
242+
// Reset input.
243+
iv.Clear()
244+
245+
// Reset cursor.
246+
err = iv.SetCursor(0, 0)
247+
if err != nil {
248+
log.Println("Failed to set cursor:", err)
249+
}
250+
return err
251+
}
252+
return nil
253+
})
254+
if err != nil {
255+
log.Println("Cannot bind the enter key:", err)
256+
}
257+
```
258+
12. For reference you can print the PubNub version info at the start of our main() function.
259+
```go
260+
// Print version info.
261+
fmt.Println("PubNub SDK for go;", messaging.VersionInfo())
262+
```
263+
13. Try it out `go run main.go`.
221264

222-
223265

266+
![Image of Go-Chat](https://raw.githubusercontent.com/chandler767/Go-Chat/master/images/chat.png)

0 commit comments

Comments
 (0)