Skip to content

Commit 6a34d28

Browse files
authored
Merge pull request #564 from talkjs/chore/update-nodejs-example
Update Node.js tutorial
2 parents 6bfc3a5 + 044006a commit 6a34d28

File tree

5 files changed

+70
-88
lines changed

5 files changed

+70
-88
lines changed

nodejs/basic-example/talkjs-backend/file.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

nodejs/basic-example/talkjs-backend/server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import bodyParser from "body-parser";
55

66
import { LowSync, JSONFileSync } from "lowdb";
77

8-
const adapter = new JSONFileSync("file.json");
8+
const adapter = new JSONFileSync("users.json");
99
const db = new LowSync(adapter);
1010
db.read();
1111
db.data ||= { users: [] };
@@ -23,7 +23,6 @@ app.post("/createUser", (req, res) => {
2323
const email = req.body.email;
2424
const photoUrl = req.body.photoUrl;
2525
const role = req.body.role;
26-
console.log(id, name, email, photoUrl, role);
2726
db.data.users.push({
2827
id: id,
2928
name: name,
@@ -47,4 +46,4 @@ app.get("/getUser/:id", (req, res) => {
4746
}
4847
});
4948

50-
app.listen(port, () => console.log(`Server up and running at port ${port}!`));
49+
app.listen(port, () => console.log(`Server up and running on port ${port}`));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"users": [
3+
{
4+
"id": "1",
5+
"name": "Alice",
6+
"email": "[email protected]",
7+
"photoUrl": "https://talkjs.com/new-web/avatar-7.jpg",
8+
"role": "default",
9+
"welcomeMessage": "Hi 👋"
10+
},
11+
{
12+
"id": "2",
13+
"name": "Sebastian",
14+
"email": "[email protected]",
15+
"photoUrl": "https://talkjs.com/new-web/avatar-2.jpg",
16+
"role": "default",
17+
"welcomeMessage": "Hello there!"
18+
}
19+
]
20+
}
Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
11
<!DOCTYPE html>
22
<html>
3-
4-
<head>
5-
<meta charset="utf-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
76
<title>TalkJS Node JS Integration</title>
8-
<meta name="description" content="">
9-
<meta name="viewport" content="width=device-width, initial-scale=1">
10-
</head>
11-
12-
<body>
13-
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
</head>
1410

11+
<body>
1512
<script src="script.js" async defer></script>
1613
<script>
17-
(function (t, a, l, k, j, s) {
18-
s = a.createElement('script');
19-
s.async = 1;
20-
s.src = "https://cdn.talkjs.com/talk.js";
21-
a.head.appendChild(s);
22-
k = t.Promise;
23-
t.Talk = {
24-
v: 3,
25-
ready: {
26-
then: function (f) {
27-
if (k) return new k(function (r, e) {
28-
l.push([f, r, e])
29-
});
30-
l
31-
.push([f])
32-
},
33-
catch: function () {
34-
return k && new k()
35-
},
36-
c: l
37-
}
38-
};
39-
})(window, document, []);
14+
(function (t, a, l, k, j, s) {
15+
s = a.createElement("script");
16+
s.async = 1;
17+
s.src = "https://cdn.talkjs.com/talk.js";
18+
a.head.appendChild(s);
19+
k = t.Promise;
20+
t.Talk = {
21+
v: 3,
22+
ready: {
23+
then: function (f) {
24+
if (k)
25+
return new k(function (r, e) {
26+
l.push([f, r, e]);
27+
});
28+
l.push([f]);
29+
},
30+
catch: function () {
31+
return k && new k();
32+
},
33+
c: l,
34+
},
35+
};
36+
})(window, document, []);
4037
</script>
41-
<script src = "https://unpkg.com/axios/dist/axios.min.js"></script>
4238
<!-- container element in which TalkJS will display a chat UI -->
4339
<div id="talkjs-container" style="width: 90%; margin: 30px; height: 500px">
44-
<i>Loading chat...</i>
40+
<i>Loading chat...</i>
4541
</div>
46-
</body>
47-
48-
</html>
42+
</body>
43+
</html>
Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
const getAgent = async () => {
2-
const response = await fetch("http://127.0.0.1:3000/getUser/1");
3-
const data = await response.json();
4-
let agent = new Talk.User({
5-
id: data.id,
6-
name: data.name,
7-
photoUrl: data.dp,
8-
email: data.email,
9-
role: data.role,
10-
});
11-
return agent;
12-
};
13-
const getUser = async () => {
14-
const response = await fetch("http://127.0.0.1:3000/getUser/2");
1+
const getUser = async (id) => {
2+
const response = await fetch(`http://127.0.0.1:3000/getUser/${id}`);
153
const data = await response.json();
164
let user = new Talk.User({
175
id: data.id,
186
name: data.name,
19-
photoUrl: data.dp,
7+
photoUrl: data.photoUrl,
208
email: data.email,
219
role: data.role,
2210
});
@@ -25,26 +13,24 @@ const getUser = async () => {
2513

2614
(async function () {
2715
await Talk.ready;
28-
let agent = await getAgent();
29-
let user = await getUser();
16+
const alice = await getUser(1);
17+
const sebastian = await getUser(2);
3018
const session = new Talk.Session({
31-
appId: "<APP_ID>",
32-
me: user,
19+
appId: "<APP_ID>", // replace with your app ID
20+
me: sebastian,
3321
});
34-
var conversation = session.getOrCreateConversation(
35-
Talk.oneOnOneId(user, agent)
22+
const conversation = session.getOrCreateConversation(
23+
"nodeJSExampleConversation"
3624
);
3725
conversation.setAttributes({
3826
welcomeMessages: [
39-
"You can start typing your message here and one of our agents will be with you shortly.",
40-
"Please do not divulge any of your personal information.",
27+
"Example chat for our Node.js tutorial. Try sending a message!",
4128
],
4229
});
43-
conversation.setParticipant(user);
44-
conversation.setParticipant(agent);
45-
console.log(conversation);
30+
conversation.setParticipant(alice);
31+
conversation.setParticipant(sebastian);
4632

47-
var inbox = session.createInbox(conversation);
48-
inbox.select(conversation);
49-
inbox.mount(document.getElementById("talkjs-container"));
33+
const chatbox = session.createChatbox(conversation);
34+
chatbox.select(conversation);
35+
chatbox.mount(document.getElementById("talkjs-container"));
5036
})();

0 commit comments

Comments
 (0)