Skip to content

Commit d8e503d

Browse files
committedMay 1, 2024
Update README
1 parent 31a25ec commit d8e503d

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed
 

‎README.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Webchat server that talks to [Courier](https://github.com/nyaruka/courier/).
77
To start chat session as new user:
88

99
```javascript
10-
sock = new WebSocket("ws://localhost:8070/start/a204047b-5224-4b8b-a328-08a538f1b3cb/");
10+
sock = new WebSocket("ws://localhost:8070/connect/7d62d551-3030-4100-a260-2d7c4e9693e7/");
1111

1212
sock.onclose = function (event) {
1313
console.log("bye!");
@@ -17,60 +17,60 @@ sock.onmessage = function (event) {
1717
};
1818
```
1919

20-
## Server Events
20+
## Client Commands
2121

22-
The first message the client will receive will contain the new chat ID:
22+
### `start_chat`
23+
24+
Can be used to start a new chat session as a new contact:
2325

2426
```json
2527
{
26-
"type": "chat_started",
27-
"chat_id": "65vbbDAQCdPdEWlEhDGy4utO"
28+
"type": "start_chat"
2829
}
2930
```
3031

31-
The client can store that identifier to reconnect as the same contact in future. Pass it as a param to the start endpoint:
32-
33-
```javascript
34-
sock = new WebSocket("ws://localhost:8070/start/a204047b-5224-4b8b-a328-08a538f1b3cb/?chat_id=65vbbDAQCdPdEWlEhDGy4utO")
32+
```json
33+
{
34+
"type": "chat_started",
35+
"chat_id": "65vbbDAQCdPdEWlEhDGy4utO"
36+
}
3537
```
3638

37-
And in this case the first message the client will receive will be:
39+
Or resume a chat session as an existing contact:
3840

3941
```json
4042
{
41-
"type": "chat_resumed",
42-
"chat_id": "65vbbDAQCdPdEWlEhDGy4utO",
43-
"email": ""
43+
"type": "start_chat",
44+
"chat_id": "65vbbDAQCdPdEWlEhDGy4utO"
4445
}
4546
```
4647

47-
Messages from courier are sent to the client as events that look like:
48-
4948
```json
5049
{
51-
"type": "msg_out",
52-
"text": "Hello there!",
53-
"origin": "flow"
50+
"type": "chat_resumed",
51+
"chat_id": "65vbbDAQCdPdEWlEhDGy4utO",
52+
"email": ""
5453
}
5554
```
5655

57-
## Client Events
56+
### `create_msg`
5857

59-
To send a message from the client, send a `msg_in` event to the socket, e.g.
58+
Creates a new message from the client:
6059

6160
```json
6261
{
63-
"type": "msg_in",
64-
"text": "Thanks!"
62+
"type": "create_msg",
63+
"text": "I need help!"
6564
}
6665
```
6766

68-
The client can collect an email address by sending a `email_added` event, e.g.
67+
### `set_email`
68+
69+
Updates the email address for the current contact:
6970

7071
```json
7172
{
72-
"type": "email_added",
73+
"type": "set_email",
7374
"email": "bob@nyaruka.com"
7475
}
7576
```
76-

‎service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (s *Service) sender() {
102102

103103
for {
104104
// TODO panic recovery
105-
// s.send()
105+
s.send()
106106

107107
select {
108108
case <-s.senderStop:

‎web/client.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
)
1919

2020
type Client struct {
21-
clientID string
22-
server *Server
23-
socket httpx.WebSocket
24-
channel models.Channel
25-
contact *models.Contact
21+
id string
22+
server *Server
23+
socket httpx.WebSocket
24+
channel models.Channel
25+
contact *models.Contact
2626

2727
send chan events.Event
2828
sendStop chan bool
@@ -31,10 +31,10 @@ type Client struct {
3131

3232
func NewClient(s *Server, sock httpx.WebSocket, channel models.Channel) *Client {
3333
c := &Client{
34-
clientID: string(uuids.New()),
35-
server: s,
36-
socket: sock,
37-
channel: channel,
34+
id: string(uuids.New()),
35+
server: s,
36+
socket: sock,
37+
channel: channel,
3838

3939
send: make(chan events.Event, 16),
4040
sendStop: make(chan bool),
@@ -49,10 +49,6 @@ func NewClient(s *Server, sock httpx.WebSocket, channel models.Channel) *Client
4949
return c
5050
}
5151

52-
func (c *Client) Channel() models.Channel {
53-
return c.channel
54-
}
55-
5652
func (c *Client) onMessage(msg []byte) {
5753
log := c.log()
5854

@@ -130,6 +126,8 @@ func (c *Client) onCommand(cmd commands.Command) error {
130126
if err := c.contact.UpdateEmail(ctx, c.server.rt, typed.Email); err != nil {
131127
return errors.Wrap(err, "error updating email")
132128
}
129+
default:
130+
log.Debug("unknown command", "type", cmd.Type())
133131
}
134132

135133
return nil
@@ -175,5 +173,5 @@ func (c *Client) chatID() models.ChatID {
175173
}
176174

177175
func (c *Client) log() *slog.Logger {
178-
return slog.With("client_id", c.clientID, "channel", c.channel.UUID(), "chat_id", c.chatID())
176+
return slog.With("client_id", c.id, "channel", c.channel.UUID(), "chat_id", c.chatID())
179177
}

‎web/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ func (s *Server) handleConnect(ctx context.Context, r *http.Request, w http.Resp
125125
client := NewClient(s, sock, ch)
126126

127127
s.clientMutex.Lock()
128-
s.clients[client.clientID] = client
128+
s.clients[client.id] = client
129129
total := len(s.clients)
130130
s.clientMutex.Unlock()
131131
s.wg.Add(1)
132132

133-
slog.Info("client connected", "channel", ch.UUID(), "client_id", client.clientID, "total", total)
133+
slog.Info("client connected", "channel", ch.UUID(), "client_id", client.id, "total", total)
134134
}
135135

136136
type sendRequest struct {
@@ -188,7 +188,7 @@ func (s *Server) GetClient(chatID models.ChatID) *Client {
188188

189189
func (s *Server) OnDisconnect(c *Client) {
190190
s.clientMutex.Lock()
191-
delete(s.clients, c.clientID)
191+
delete(s.clients, c.id)
192192
total := len(s.clients)
193193
s.clientMutex.Unlock()
194194
s.wg.Done()

0 commit comments

Comments
 (0)