Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More updates to Node.js example code #565

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion nodejs/basic-example/talkjs-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions nodejs/basic-example/talkjs-backend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "integrating-a-nodejs-app-with-talkjs",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "server.js",
"scripts": {
Expand All @@ -12,7 +11,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.2",
"cors": "^2.8.5",
"express": "^4.17.3",
"lowdb": "^3.0.0"
Expand Down
10 changes: 4 additions & 6 deletions nodejs/basic-example/talkjs-backend/server.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import express from "express";

import cors from "cors";
import bodyParser from "body-parser";

import { LowSync, JSONFileSync } from "lowdb";

const adapter = new JSONFileSync("users.json");
const db = new LowSync(adapter);
db.read();
// Initialize with an empty array if users.json is empty or doesn't exist
db.data ||= { users: [] };

const app = express();
const port = 3000;
app.use(cors());

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json());

app.post("/createUser", (req, res) => {
const id = req.body.id;
const name = req.body.name;
const email = req.body.email;
const photoUrl = req.body.photoUrl;
const role = req.body.role;
const welcomeMessage = req.body.welcomeMessage;
db.data.users.push({
id: id,
name: name,
email: email,
photoUrl: photoUrl,
role: role,
welcomeMessage: welcomeMessage,
});
db.write();
res.status(200).send("User created successfully");
Expand Down
4 changes: 2 additions & 2 deletions nodejs/basic-example/talkjs-backend/users.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"users": [
{
"id": "1",
"id": "alice",
"name": "Alice",
"email": "[email protected]",
"photoUrl": "https://talkjs.com/new-web/avatar-7.jpg",
"role": "default",
"welcomeMessage": "Hi 👋"
},
{
"id": "2",
"id": "sebastian",
"name": "Sebastian",
"email": "[email protected]",
"photoUrl": "https://talkjs.com/new-web/avatar-2.jpg",
Expand Down
4 changes: 2 additions & 2 deletions nodejs/basic-example/talkjs-frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>TalkJS Node JS Integration</title>
<title>TalkJS with NodeJS example</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<body>
<script src="script.js" async defer></script>
<script>
(function (t, a, l, k, j, s) {
s = a.createElement("script");
Expand All @@ -35,6 +34,7 @@
};
})(window, document, []);
</script>
<script src="script.js"></script>
<!-- container element in which TalkJS will display a chat UI -->
<div id="talkjs-container" style="width: 90%; margin: 30px; height: 500px">
<i>Loading chat...</i>
Expand Down
5 changes: 3 additions & 2 deletions nodejs/basic-example/talkjs-frontend/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ const getUser = async (id) => {
photoUrl: data.photoUrl,
email: data.email,
role: data.role,
welcomeMessage: data.welcomeMessage,
});
return user;
};

(async function () {
await Talk.ready;
const alice = await getUser(1);
const sebastian = await getUser(2);
const alice = await getUser("alice");
const sebastian = await getUser("sebastian");
const session = new Talk.Session({
appId: "<APP_ID>", // replace with your app ID
me: sebastian,
Expand Down