Project Templates
Social Network
How to Create a Social Media Platform: Step-by-Step Guide (2025)
67 นาที
บทนำ ในบทเรียนนี้ คุณจะได้เรียนรู้วิธีการสร้างแพลตฟอร์มเครือข่ายสังคมเช่น instagram โดยใช้ back4app เป็นบริการแบ็คเอนด์ของคุณ back4app ให้บริการ parse server ที่จัดการซึ่งทำให้การตรวจสอบสิทธิ์ผู้ใช้ การจัดเก็บข้อมูล การอัปโหลดไฟล์ และฟีเจอร์เรียลไทม์ง่ายขึ้นโดยไม่ต้องการโครงสร้างเซิร์ฟเวอร์ที่ซับซ้อน เมื่อคุณทำบทเรียนนี้เสร็จ คุณจะสร้างเครือข่ายสังคมที่สมบูรณ์ด้วย การตรวจสอบสิทธิ์ผู้ใช้ (ลงทะเบียน, เข้าสู่ระบบ, รีเซ็ตรหัสผ่าน) การจัดการโปรไฟล์ การสร้างโพสต์พร้อมการอัปโหลดภาพ การมีปฏิสัมพันธ์ทางสังคม (ถูกใจ, ความคิดเห็น) การส่งข้อความเรียลไทม์พร้อมตัวบ่งชี้การพิมพ์ ฟังก์ชันการค้นหาข้อมูล ตัวอย่างเครือข่ายสังคมอินเตอร์เฟซแชทโปรไฟล์ผู้ใช้หน้า feed คุณสามารถเข้าถึงโค้ดทั้งหมดได้ที่ github ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้องมี บัญชี back4app ลงทะเบียนสำหรับบัญชีฟรีที่ back4app com https //www back4app com/ ติดตั้ง node js และ npm บนเครื่องของคุณ ติดตั้ง node js (เวอร์ชัน 14 x หรือใหม่กว่า) และ npm จาก nodejs org https //nodejs org/ ความเข้าใจพื้นฐานเกี่ยวกับ javascript และ react โปรแกรมแก้ไขโค้ด โปรแกรมแก้ไขโค้ดสมัยใหม่ใด ๆ เช่น visual studio code หรือ sublime text ขั้นตอนที่ 1 — ตั้งค่าพื้นหลัง back4app ของคุณ ก่อนอื่น มาสร้างโปรเจ็กต์ back4app ใหม่และตั้งค่ารูปแบบฐานข้อมูลสำหรับเครือข่ายสังคมของเรา สร้างโปรเจกต์ back4app ใหม่ เข้าสู่ระบบบัญชี back4app ของคุณและไปที่แดชบอร์ด คลิกที่ "สร้างแอปใหม่" ใส่ "back4gram" เป็นชื่อแอปของคุณ เลือกภูมิภาคเซิร์ฟเวอร์ที่ใกล้ที่สุด และคลิก "สร้าง" เข้าใจโครงสร้างฐานข้อมูล เครือข่ายสังคมของเราต้องการคลาสต่อไปนี้ใน back4app ผู้ใช้ (มีอยู่แล้วตามค่าเริ่มต้นใน parse) จะมีการขยายด้วยฟิลด์เพิ่มเติมเช่น bio และ avatar โพสต์ เก็บโพสต์ของผู้ใช้รวมถึงเนื้อหาข้อความและภาพ ความคิดเห็น เก็บความคิดเห็นเกี่ยวกับโพสต์ การสนทนา แสดงถึงการสนทนาผ่านแชทระหว่างผู้ใช้ ข้อความ ข้อความแต่ละข้อความภายในการสนทนา สถานะการพิมพ์ ติดตามเมื่อผู้ใช้กำลังพิมพ์ในบทสนทนา สร้างคลาสฐานข้อมูล มาสร้างคลาสเหล่านี้ในฐานข้อมูล back4app ของคุณกันเถอะ ไปที่ส่วน "ฐานข้อมูล" ในแดชบอร์ด back4app ของคุณ การขยายคลาสผู้ใช้ คลิกที่คลาส "user" ที่มีอยู่แล้ว เพิ่มคอลัมน์ต่อไปนี้ ชีวประวัติ (ประเภท สตริง) อวตาร (ประเภท ไฟล์) ผู้ติดตาม (ประเภท หมายเลข, ค่าเริ่มต้น 0) ตาม (ประเภท หมายเลข, ค่าเริ่มต้น 0) สร้างคลาสโพสต์ คลิก "สร้างชั้นเรียน" ป้อน "post" เป็นชื่อคลาสและเลือก "สร้างคลาสว่าง" เพิ่มคอลัมน์ต่อไปนี้ เนื้อหา (ประเภท สตริง) ผู้เขียน (ประเภท ชี้ไปที่ ผู้ใช้) ภาพ (ประเภท ไฟล์) ชอบ (ประเภท หมายเลข, ค่าเริ่มต้น 0) ถูกใจโดย (ประเภท อาร์เรย์) สร้างเมื่อ (ประเภท วันที่, เพิ่มโดยอัตโนมัติ) การสร้างคลาส comment สร้างคลาสใหม่ชื่อ "comment" โดยมีคอลัมน์เหล่านี้ เนื้อหา (ประเภท สตริง) ผู้เขียน (ประเภท ชี้ไปที่ ผู้ใช้) โพสต์ (ประเภท ชี้ไปที่โพสต์) สร้างเมื่อ (ประเภท วันที่, เพิ่มโดยอัตโนมัติ) การสร้างคลาสการสนทนา สร้างคลาสใหม่ชื่อ "conversation" โดยมีคอลัมน์เหล่านี้ ผู้เข้าร่วม (ประเภท อาร์เรย์) ข้อความสุดท้าย (ประเภท สตริง) อัปเดตเมื่อ (ประเภท วันที่, เพิ่มโดยอัตโนมัติ) สร้างคลาสข้อความ สร้างคลาสใหม่ชื่อ "message" โดยมีคอลัมน์เหล่านี้ ข้อความ (ประเภท สตริง) ผู้ส่ง (ประเภท ตัวชี้ไปยัง ผู้ใช้) การสนทนา (ประเภท ชี้ไปที่การสนทนา) สร้างเมื่อ (ประเภท วันที่, เพิ่มโดยอัตโนมัติ) การสร้างคลาส typingstatus สร้างคลาสใหม่ชื่อ "typingstatus" โดยมีคอลัมน์เหล่านี้ ผู้ใช้ (ประเภท ตัวชี้ไปยัง ผู้ใช้) การสนทนา (ประเภท ตัวชี้ไปยังการสนทนา) กำลังพิมพ์ (ประเภท บูลีน) การตั้งค่าการอนุญาตของคลาส เพื่อรักษาความปลอดภัยข้อมูลแอปพลิเคชันของคุณ ให้กำหนดรายการควบคุมการเข้าถึง (acls) ที่เหมาะสมสำหรับแต่ละคลาส ไปที่ส่วน "ความปลอดภัย & คีย์" ในแดชบอร์ด back4app ของคุณ ภายใต้ "ความปลอดภัยระดับคลาส" ให้กำหนดสิทธิ์สำหรับแต่ละคลาส ตัวอย่างเช่น ใน คลาสโพสต์ การเข้าถึงการอ่านสาธารณะ เปิดใช้งาน (ทุกคนสามารถเห็นโพสต์) การเข้าถึงการเขียนสาธารณะ เปิดใช้งาน (ผู้ใช้ที่ผ่านการตรวจสอบแล้วสามารถสร้างโพสต์) เพิ่ม clps สำหรับการอัปเดต/ลบเพื่อจำกัดเฉพาะผู้เขียนเท่านั้น การตั้งค่า livequery สำหรับฟีเจอร์เรียลไทม์ เพื่อเปิดใช้งานฟีเจอร์เรียลไทม์ เช่น การส่งข้อความและตัวบ่งชี้การพิมพ์ ไปที่ "การตั้งค่าเซิร์ฟเวอร์" ในแดชบอร์ด back4app ภายใต้ "parse server" ให้ค้นหา "livequery" และเปิดใช้งานมัน เพิ่มคลาสเหล่านี้เพื่อให้ livequery ตรวจสอบ ข้อความ สถานะการพิมพ์ โพสต์ (สำหรับการอัปเดตแบบเรียลไทม์เกี่ยวกับไลค์และความคิดเห็น) การรับคีย์แอปพลิเคชันของคุณ คุณจะต้องใช้คีย์แอปพลิเคชัน back4app ของคุณเพื่อเชื่อมต่อกับส่วนหน้า ไปที่ "การตั้งค่าแอป" > "ความปลอดภัย & คีย์" จดคีย์ต่อไปนี้ application id javascript key server url livequery server url กุญแจรักษาความปลอดภัย ขั้นตอนที่ 2 — เชื่อมต่อ frontend ของคุณกับ back4app เรามาตั้งค่าการเชื่อมต่อระหว่าง frontend ของคุณกับ backend ของ back4app การสร้างตัวแปรสภาพแวดล้อม สร้างไฟล์ env local ในรากของโปรเจกต์ของคุณเพื่อเก็บข้อมูลรับรอง back4app ของคุณ react app parse app id=your application id react app parse js key=your javascript key react app parse server url=https //parseapi back4app com react app parse live query url=wss\ //your app id back4app io แทนที่ค่าตัวอย่างด้วยข้อมูลรับรอง back4app ที่แท้จริงของคุณ การกำหนดค่า parse sdk กับ back4app สร้างไฟล์การกำหนดค่าเพื่อเริ่มต้น parse ด้วยข้อมูลรับรอง back4app ของคุณ // src/utils/parseconfig js import parse from 'parse/dist/parse min js'; // initialize parse parse initialize( process env react app parse app id, process env react app parse js key ); parse serverurl = process env react app parse server url; // initialize live queries if (process env react app parse live query url) { parse livequeryserverurl = process env react app parse live query url; } export default parse; จากนั้นนำเข้าการกำหนดค่านี้ในจุดเริ่มต้นของแอปของคุณ // src/index js import react from 'react'; import reactdom from 'react dom/client'; import ' /index css'; import app from ' /app'; import ' /utils/parseconfig'; // import parse configuration const root = reactdom createroot(document getelementbyid('root')); root render( \<react strictmode> \<app /> \</react strictmode> ); ขั้นตอนที่ 3 — การดำเนินการรับรองความถูกต้องด้วย back4app parse server ของ back4app มีระบบการจัดการผู้ใช้ที่ครอบคลุมผ่าน parse user คลาส การเข้าใจการรับรองความถูกต้องของผู้ใช้ parse คลาส parse user ถูกออกแบบมาโดยเฉพาะสำหรับการจัดการผู้ใช้ เก็บข้อมูลรับรองผู้ใช้ (ชื่อผู้ใช้, อีเมล, รหัสผ่าน) จัดการสถานะการรับรองความถูกต้อง จัดการโทเค็นเซสชันโดยอัตโนมัติ การดำเนินการลงทะเบียนผู้ใช้ นี่คือวิธีการดำเนินการลงทะเบียนผู้ใช้ด้วย parse // signup function const handlesignup = async () => { try { // create a new user const user = new parse user(); user set('username', username); user set('email', email); user set('password', password); // additional user data user set('bio', ''); user set('followers', 0); user set('following', 0); // sign up the user await user signup(); // success user is automatically logged in console log('user registered successfully'); // navigate to feed or home page navigate('/feed'); } catch (error) { // handle specific parse errors console error('error signing up ', error message); if (error code === 202) { seterrors({ errors, username 'username already taken'}); } else if (error code === 203) { seterrors({ errors, email 'email already in use'}); } } }; การนำไปใช้การเข้าสู่ระบบของผู้ใช้ นี่คือวิธีการนำไปใช้การเข้าสู่ระบบของผู้ใช้ด้วย parse // login function const handlelogin = async () => { try { // log in the user const user = await parse user login(username, password); // success user is logged in console log('user logged in successfully ', user getusername()); // navigate to feed or home page navigate('/feed'); } catch (error) { // handle login errors console error('error logging in ', error message); setloginerror(error message); } }; การตรวจสอบผู้ใช้ปัจจุบัน parse จะเก็บโทเค็นเซสชันโดยอัตโนมัติ ทำให้คุณสามารถตรวจสอบได้ว่าผู้ใช้ได้เข้าสู่ระบบแล้วหรือไม่ // check if user is already logged in const checkcurrentuser = async () => { try { const currentuser = await parse user current(); if (currentuser) { console log('current user ', currentuser getusername()); return currentuser; } return null; } catch (error) { console error('error checking current user ', error); return null; } }; การดำเนินการรีเซ็ตรหัสผ่าน back4app มีฟังก์ชันการรีเซ็ตรหัสผ่านในตัว // password reset function const handlepasswordreset = async () => { try { await parse user requestpasswordreset(email); // success email sent to user console log('password reset email sent'); setresetemailsent(true); } catch (error) { // handle reset errors console error('error requesting password reset ', error message); setreseterror(error message); } }; ขั้นตอนที่ 4 — การสร้างและแสดงโพสต์ ตอนนี้เรามาดำเนินการสร้างและดึงโพสต์โดยใช้ back4app การสร้างโพสต์ นี่คือวิธีการสร้างโพสต์ใหม่ด้วย parse // create post function const createpost = async () => { if (!postcontent trim() && !postimage) { console error('post must have content or image'); return; } try { // create a new post object const post = parse object extend('post'); const newpost = new post(); // set post content newpost set('content', postcontent); newpost set('author', parse user current()); newpost set('likes', 0); newpost set('likedby', \[]); // if there's an image, upload it if (postimage) { const parsefile = new parse file(postimage name, postimage); await parsefile save(); newpost set('image', parsefile); } // save the post await newpost save(); console log('post created successfully'); return newpost; } catch (error) { console error('error creating post ', error message); throw error; } }; กลไกหลักของ back4app parse object extend('post') อ้างอิงถึงคลาส post ใน back4app new post() สร้างอินสแตนซ์ใหม่ของคลาส post parsefile save() อัปโหลดไฟล์ไปยังที่เก็บของ back4app newpost save() บันทึกอ็อบเจ็กต์โพสต์ไปยัง back4app การดึงโพสต์ นี่คือวิธีการดึงโพสต์จาก back4app // fetch posts function const fetchposts = async (page = 0, limit = 10) => { try { // create a query for the post class const post = parse object extend('post'); const query = new parse query(post); // include the author object (pointer) query include('author'); // sort by creation date, newest first query descending('createdat'); // pagination query limit(limit); query skip(page limit); // execute the query const results = await query find(); // process the results const posts = results map(post => ({ id post id, content post get('content'), image post get('image') ? post get('image') url() null, likes post get('likes') || 0, likedby post get('likedby') || \[], createdat post get('createdat'), author { id post get('author') id, username post get('author') get('username'), avatar post get('author') get('avatar') ? post get('author') get('avatar') url() null } })); return posts; } catch (error) { console error('error fetching posts ', error); throw error; } }; กลไกหลักของ back4app new parse query(post) สร้างการค้นหาบนคลาส post query include('author') ทำการรวมข้อมูลเพื่อดึงวัตถุที่เกี่ยวข้อง query descending('createdat') จัดเรียงผลลัพธ์ตามวันที่สร้าง query limit() และ query skip() ใช้การแบ่งหน้า post get('image') url() ดึง url ของวัตถุ parse file การนำฟังก์ชันถูกใจไปใช้ นี่คือวิธีการนำการถูกใจโพสต์ไปใช้ // like/unlike post function const togglelike = async (postid) => { try { const currentuser = parse user current(); const userid = currentuser id; // get the post object const post = parse object extend('post'); const query = new parse query(post); const post = await query get(postid); // get current likes and likedby array const likedby = post get('likedby') || \[]; const isliked = likedby includes(userid); // update likes count and likedby array if (isliked) { // unlike remove user from likedby and decrement likes post set('likedby', likedby filter(id => id !== userid)); post set('likes', math max((post get('likes') || 1) 1, 0)); } else { // like add user to likedby and increment likes post set('likedby', \[ likedby, userid]); post set('likes', (post get('likes') || 0) + 1); } // save the updated post await post save(); return !isliked; // return new like status } catch (error) { console error('error toggling like ', error); throw error; } }; ขั้นตอนที่ 5 — การนำความคิดเห็นไปใช้ในโพสต์ มาดำเนินการฟังก์ชันความคิดเห็นโดยใช้ back4app กันเถอะ การสร้างความคิดเห็น นี่คือวิธีการเพิ่มความคิดเห็นในโพสต์ // add comment function const addcomment = async (postid, commentcontent) => { if (!commentcontent trim()) { console error('comment cannot be empty'); return; } try { // get the post object const post = parse object extend('post'); const postquery = new parse query(post); const post = await postquery get(postid); // create a new comment object const comment = parse object extend('comment'); const comment = new comment(); // set comment data comment set('content', commentcontent); comment set('author', parse user current()); comment set('post', post); // save the comment await comment save(); console log('comment added successfully'); return comment; } catch (error) { console error('error adding comment ', error); throw error; } }; การดึงความคิดเห็นสำหรับโพสต์ นี่คือวิธีการดึงความคิดเห็นสำหรับโพสต์เฉพาะ // fetch comments function const fetchcomments = async (postid) => { try { // get the post object const post = parse object extend('post'); const postquery = new parse query(post); const post = await postquery get(postid); // create a query for comments const comment = parse object extend('comment'); const query = new parse query(comment); // find comments for this post query equalto('post', post); // include the author information query include('author'); // sort by creation date query ascending('createdat'); // execute the query const results = await query find(); // process the results const comments = results map(comment => ({ id comment id, content comment get('content'), createdat comment get('createdat'), author { id comment get('author') id, username comment get('author') get('username'), avatar comment get('author') get('avatar') ? comment get('author') get('avatar') url() null } })); return comments; } catch (error) { console error('error fetching comments ', error); throw error; } }; ขั้นตอนที่ 6 — การนำโปรไฟล์ผู้ใช้ไปใช้ ตอนนี้เรามานำโปรไฟล์ผู้ใช้ไปใช้โดยใช้ back4app การดึงข้อมูลผู้ใช้ นี่คือวิธีการดึงข้อมูลโปรไฟล์ผู้ใช้ // fetch user profile function const fetchuserprofile = async (userid) => { try { // create a query for the user class const query = new parse query(parse user); // get the user by id const user = await query get(userid); // get user posts const post = parse object extend('post'); const postsquery = new parse query(post); postsquery equalto('author', user); postsquery include('author'); postsquery descending('createdat'); const posts = await postsquery find(); // process user data const userdata = { id user id, username user get('username'), bio user get('bio') || '', avatar user get('avatar') ? user get('avatar') url() null, followers user get('followers') || 0, following user get('following') || 0, posts posts map(post => ({ id post id, content post get('content'), image post get('image') ? post get('image') url() null, likes post get('likes') || 0, createdat post get('createdat') })) }; return userdata; } catch (error) { console error('error fetching user profile ', error); throw error; } }; การอัปเดตโปรไฟล์ผู้ใช้ นี่คือวิธีการอัปเดตโปรไฟล์ของผู้ใช้ // update user profile function const updateuserprofile = async (profiledata) => { try { const currentuser = await parse user current(); // update profile fields if (profiledata bio !== undefined) { currentuser set('bio', profiledata bio); } if (profiledata avatarfile) { const parsefile = new parse file('avatar jpg', profiledata avatarfile); await parsefile save(); currentuser set('avatar', parsefile); } // save the updated user await currentuser save(); console log('profile updated successfully'); return currentuser; } catch (error) { console error('error updating profile ', error); throw error; } }; ขั้นตอนที่ 7 — การนำเสนอการส่งข้อความแบบเรียลไทม์ด้วย livequery ตอนนี้เรามาใช้ฟีเจอร์ livequery ของ back4app เพื่อสร้างการส่งข้อความแบบเรียลไทม์กันเถอะ การสร้างการสนทนา นี่คือวิธีการสร้างการสนทนาใหม่ // create conversation function const createconversation = async (participantids) => { try { // ensure current user is included in participants const currentuser = await parse user current(); const allparticipantids = \[ new set(\[currentuser id, participantids])]; // check if conversation already exists const existingconversation = await findexistingconversation(allparticipantids); if (existingconversation) { return existingconversation; } // get user objects for all participants const participantpointers = await promise all( allparticipantids map(async (id) => { const userquery = new parse query(parse user); return await userquery get(id); }) ); // create new conversation const conversation = parse object extend('conversation'); const conversation = new conversation(); conversation set('participants', participantpointers); conversation set('lastmessage', ''); await conversation save(); console log('conversation created successfully'); return conversation; } catch (error) { console error('error creating conversation ', error); throw error; } }; // helper to find existing conversation const findexistingconversation = async (participantids) => { try { const conversation = parse object extend('conversation'); const query = new parse query(conversation); // this is a simplified approach in production you'd need more complex query const results = await query find(); for (const conversation of results) { const participants = conversation get('participants') || \[]; const participantids = participants map(p => p id); // check if arrays have same elements (regardless of order) if (participantids length === participantids length && participantids every(id => participantids includes(id))) { return conversation; } } return null; } catch (error) { console error('error finding existing conversation ', error); return null; } }; การส่งข้อความ นี่คือวิธีการส่งข้อความในสนทนา // send message function const sendmessage = async (conversationid, messagetext) => { try { const currentuser = parse user current(); // get the conversation const conversation = parse object extend('conversation'); const conversationquery = new parse query(conversation); const conversation = await conversationquery get(conversationid); // create a new message const message = parse object extend('message'); const message = new message(); message set('text', messagetext); message set('sender', currentuser); message set('conversation', conversation); message set('read', false); await message save(); // update conversation with last message conversation set('lastmessage', messagetext); await conversation save(); console log('message sent successfully'); return message; } catch (error) { console error('error sending message ', error); throw error; } }; การตั้งค่า livequery สำหรับข้อความ นี่คือวิธีการสมัครรับข้อมูลการอัปเดตข้อความแบบเรียลไทม์ // subscribe to messages function const subscribetomessages = async (conversationid, onnewmessage) => { try { // create a query for messages in this conversation const message = parse object extend('message'); const query = new parse query(message); // get the conversation pointer const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; // set up query constraints query equalto('conversation', conversationpointer); query include('sender'); // subscribe to the query const subscription = await query subscribe(); // listen for create events (new messages) subscription on('create', (message) => { // process the message const newmessage = { id message id, text message get('text'), createdat message get('createdat'), sender { id message get('sender') id, username message get('sender') get('username'), avatar message get('sender') get('avatar') ? message get('sender') get('avatar') url() null }, read message get('read') }; // call the callback with the new message onnewmessage(newmessage); // mark the message as read if from another user if (message get('sender') id !== parse user current() id) { markmessageasread(message); } }); return subscription; } catch (error) { console error('error subscribing to messages ', error); throw error; } }; // helper to mark message as read const markmessageasread = async (message) => { try { message set('read', true); await message save(); } catch (error) { console error('error marking message as read ', error); } }; การนำเสนอการพิมพ์ นี่คือวิธีการนำเสนอการพิมพ์ด้วย livequery // update typing status function const updatetypingstatus = async (conversationid, istyping) => { try { const currentuser = parse user current(); // get the conversation pointer const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; // check if typing status exists const typingstatus = parse object extend('typingstatus'); const query = new parse query(typingstatus); query equalto('conversation', conversationpointer); query equalto('user', currentuser); let typingstatus = await query first(); if (typingstatus) { // update existing status typingstatus set('istyping', istyping); } else { // create new status typingstatus = new typingstatus(); typingstatus set('conversation', conversationpointer); typingstatus set('user', currentuser); typingstatus set('istyping', istyping); } await typingstatus save(); } catch (error) { console error('error updating typing status ', error); } }; // subscribe to typing status function const subscribetotypingstatus = async (conversationid, ontypingstatuschange) => { try { // create a query for typing status in this conversation const typingstatus = parse object extend('typingstatus'); const query = new parse query(typingstatus); // get the conversation pointer const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; // set up query constraints query equalto('conversation', conversationpointer); query include('user'); // subscribe to the query const subscription = await query subscribe(); // listen for update events subscription on('update', (typingstatus) => { const user = typingstatus get('user'); const istyping = typingstatus get('istyping'); // call the callback with typing status ontypingstatuschange({ userid user id, username user get('username'), istyping }); }); // also listen for create events subscription on('create', (typingstatus) => { const user = typingstatus get('user'); const istyping = typingstatus get('istyping'); // call the callback with typing status ontypingstatuschange({ userid user id, username user get('username'), istyping }); }); return subscription; } catch (error) { console error('error subscribing to typing status ', error); throw error; } }; ขั้นตอนที่ 8 — การนำเสนอฟังก์ชันการค้นหา เรามานำเสนอฟังก์ชันการค้นหาด้วยระบบการค้นหาของ back4app กันเถอะ การค้นหาผู้ใช้ นี่คือวิธีการค้นหาผู้ใช้ // search users function const searchusers = async (query, limit = 20) => { try { // create a query on the user class const userquery = new parse query(parse user); // search by username (case insensitive) userquery matches('username', new regexp(query, 'i')); // limit results to improve performance userquery limit(limit); // execute the query const users = await userquery find(); // process the results const userresults = users map(user => ({ id user id, username user get('username'), avatar user get('avatar') ? user get('avatar') url() null, bio user get('bio') || '' })); return userresults; } catch (error) { console error('error searching users ', error); throw error; } }; \### searching for posts here's how to search for posts by content ```javascript // search posts function const searchposts = async (query, limit = 20) => { try { // create a query on the post class const post = parse object extend('post'); const postquery = new parse query(post); // search for posts with content containing the query string postquery matches('content', new regexp(query, 'i')); // include the author information postquery include('author'); // sort by creation date (newest first) postquery descending('createdat'); // limit results postquery limit(limit); // execute the query const posts = await postquery find(); // process the results const postresults = posts map(post => ({ id post id, content post get('content'), image post get('image') ? post get('image') url() null, likes post get('likes') || 0, createdat post get('createdat'), author { id post get('author') id, username post get('author') get('username'), avatar post get('author') get('avatar') ? post get('author') get('avatar') url() null } })); return postresults; } catch (error) { console error('error searching posts ', error); throw error; } }; การค้นหาสำหรับแฮชแท็ก นี่คือวิธีการค้นหาสำหรับโพสต์ที่มีแฮชแท็กเฉพาะ // search hashtags function const searchhashtags = async (tag, limit = 20) => { try { // remove # if present at the beginning const hashtagquery = tag startswith('#') ? tag substring(1) tag; // create a query on the post class const post = parse object extend('post'); const postquery = new parse query(post); // search for posts with content containing the hashtag // using word boundaries to find actual hashtags postquery matches('content', new regexp(`#${hashtagquery}\\\b`, 'i')); // include the author information postquery include('author'); // sort by creation date (newest first) postquery descending('createdat'); // limit results postquery limit(limit); // execute the query const posts = await postquery find(); // process the results const hashtagresults = posts map(post => ({ id post id, content post get('content'), image post get('image') ? post get('image') url() null, likes post get('likes') || 0, createdat post get('createdat'), author { id post get('author') id, username post get('author') get('username'), avatar post get('author') get('avatar') ? post get('author') get('avatar') url() null } })); return hashtagresults; } catch (error) { console error('error searching hashtags ', error); throw error; } }; ขั้นตอนที่ 9 — การนำฟีเจอร์ขั้นสูงของ back4app มาใช้ มาสำรวจฟีเจอร์ขั้นสูงบางอย่างของ back4app ที่สามารถเพิ่มประสิทธิภาพให้กับแอปพลิเคชันเครือข่ายสังคมของคุณกันเถอะ ฟังก์ชันคลาวด์ back4app ช่วยให้คุณสามารถนำตรรกะฝั่งเซิร์ฟเวอร์มาใช้โดยใช้ฟังก์ชันคลาวด์ ฟังก์ชันเหล่านี้เป็นฟังก์ชัน javascript ที่ทำงานบนเซิร์ฟเวอร์และสามารถเรียกใช้จากแอปไคลเอนต์ของคุณได้ นี่คือตัวอย่างของฟังก์ชันคลาวด์สำหรับติดตามการแจ้งเตือนความคิดเห็น // in your back4app cloud code section parse cloud aftersave("comment", async (request) => { // only run for new comments, not updates if (request original) return; const comment = request object; const post = comment get("post"); const commenter = request user; // skip if user is commenting on their own post const postquery = new parse query("post"); const fullpost = await postquery get(post id, { usemasterkey true }); const postauthor = fullpost get("author"); if (postauthor id === commenter id) return; // create a notification const notification = parse object extend("notification"); const notification = new notification(); notification set("type", "comment"); notification set("fromuser", commenter); notification set("touser", postauthor); notification set("post", post); notification set("read", false); await notification save(null, { usemasterkey true }); }); ในการนำไปใช้ ไปที่แดชบอร์ด back4app ของคุณ ไปที่ "cloud code" > "cloud functions" สร้างฟังก์ชันใหม่ด้วยโค้ดด้านบน ปรับใช้ฟังก์ชัน โค้ดคลาวด์สำหรับการค้นหาขั้นสูง สำหรับฟังก์ชันการค้นหาที่ซับซ้อนมากขึ้น คุณสามารถสร้าง cloud function // in your back4app cloud code section parse cloud define("advancedsearch", async (request) => { const { query, type, limit = 20 } = request params; if (!query) { throw new error("search query is required"); } let results = \[]; switch (type) { case 'users' const userquery = new parse query(parse user); userquery matches('username', new regexp(query, 'i')); userquery limit(limit); results = await userquery find({ usemasterkey true }); break; case 'posts' const post = parse object extend('post'); const postquery = new parse query(post); postquery matches('content', new regexp(query, 'i')); postquery include('author'); postquery limit(limit); results = await postquery find({ usemasterkey true }); break; default throw new error("invalid search type"); } return results; }); เพื่อเรียกใช้ฟังก์ชันนี้จากไคลเอนต์ของคุณ // call the advanced search cloud function const calladvancedsearch = async (query, type, limit = 20) => { try { const results = await parse cloud run('advancedsearch', { query, type, limit }); return results; } catch (error) { console error('error calling advanced search ', error); throw error; } }; การดำเนินการงานเบื้องหลัง สำหรับงานที่เกิดขึ้นซ้ำ เช่น การคำนวณหัวข้อที่กำลังเป็นที่นิยม คุณสามารถใช้ background jobs // in your back4app cloud code section parse cloud job("calculatetrendingtopics", async () => { // get posts from the last 7 days const post = parse object extend("post"); const query = new parse query(post); const oneweekago = new date(); oneweekago setdate(oneweekago getdate() 7); query greaterthan('createdat', oneweekago); // limit to a reasonable number for analysis query limit(1000); const posts = await query find({ usemasterkey true }); // extract hashtags and count occurrences const hashtagcounts = {}; posts foreach(post => { const content = post get('content') || ''; const hashtags = content match(/#(\w+)/g) || \[]; hashtags foreach(hashtag => { const tag = hashtag tolowercase(); hashtagcounts\[tag] = (hashtagcounts\[tag] || 0) + 1; }); }); // convert to array and sort by count const trendingarray = object entries(hashtagcounts) map((\[hashtag, count]) => ({ hashtag, count })) sort((a, b) => b count a count) slice(0, 10); // get top 10 // save to a trendingtopics class const trendingtopics = parse object extend("trendingtopics"); const trending = new trendingtopics(); trending set("topics", trendingarray); trending set("calculatedat", new date()); await trending save(null, { usemasterkey true }); return "trending topics calculated successfully"; }); ในการตั้งค่างานนี้ ไปที่แดชบอร์ด back4app ของคุณ ไปที่ "cloud code" > "background jobs" สร้างงานใหม่ด้วยโค้ดด้านบน ตั้งเวลาให้ทำงานทุกวันหรือทุกสัปดาห์ การใช้ parse server hooks back4app ช่วยให้คุณสามารถใช้ hooks ฝั่งเซิร์ฟเวอร์ที่ทำงานโดยอัตโนมัติก่อนหรือหลังการดำเนินการบางอย่าง ซึ่งมีประโยชน์สำหรับการตรวจสอบข้อมูล การปรับเปลี่ยน หรือการกระตุ้นผลข้างเคียง // beforesave hook to sanitize post content parse cloud beforesave("post", async (request) => { const post = request object; let content = post get("content"); // sanitize content (example remove profanity) const profanitylist = \["badword1", "badword2", "badword3"]; profanitylist foreach(word => { const regex = new regexp(word, "gi"); content = content replace(regex, " "); }); // update the content post set("content", content); }); // afterdelete hook to clean up related data parse cloud afterdelete("post", async (request) => { const post = request object; const postid = post id; // delete all comments associated with this post const comment = parse object extend("comment"); const query = new parse query(comment); query equalto("post", post); const comments = await query find({ usemasterkey true }); await parse object destroyall(comments, { usemasterkey true }); console log(`deleted ${comments length} comments for post ${postid}`); }); การใช้งานการแจ้งเตือนแบบพุช back4app ให้ระบบการแจ้งเตือนแบบพุชที่แข็งแกร่งซึ่งช่วยให้คุณส่งการแจ้งเตือนไปยังอุปกรณ์ของผู้ใช้ได้ นี่คือวิธีการใช้งานการแจ้งเตือนแบบพุช ขั้นแรก ตั้งค่าการกำหนดค่าพุชในแดชบอร์ด back4app ของคุณ ไปที่ "การตั้งค่าแอป" > "พุช" กำหนดค่าข้อมูลประจำตัว ios และ/หรือ android ของคุณ ส่งการแจ้งเตือนแบบพุชเมื่อผู้ใช้ได้รับข้อความใหม่ // in your cloud code parse cloud aftersave("message", async (request) => { const message = request object; const conversation = message get("conversation"); const sender = message get("sender"); // get the conversation to find recipients const conversationquery = new parse query("conversation"); const fullconversation = await conversationquery get(conversation id, { usemasterkey true }); // get all participants const participants = fullconversation get("participants") || \[]; // send notification to all participants except the sender for (const participant of participants) { if (participant id !== sender id) { // create a query for this user's installations const pushquery = new parse query(parse installation); pushquery equalto("user", participant); // send the push notification await parse push send({ where pushquery, data { alert `new message from ${sender get("username")}`, sound "default", badge "increment", sender sender id, messageid message id, conversationid conversation id } }, { usemasterkey true }); } } }); การใช้การควบคุมการเข้าถึงตามบทบาท back4app มีระบบการควบคุมการเข้าถึงตามบทบาทที่ช่วยให้คุณจัดการสิทธิ์ได้ในระดับที่ละเอียดมากขึ้น // create an admin role const createadminrole = async (adminuser) => { try { // create a new role const adminrole = new parse role("administrator", new parse acl()); // set initial users of this role adminrole getusers() add(adminuser); // save the role await adminrole save(null, { usemasterkey true }); console log("admin role created successfully"); return adminrole; } catch (error) { console error("error creating admin role ", error); throw error; } }; // set permissions for posts to be moderated by admins const setpostmoderatorpermissions = async () => { try { // get the admin role const rolequery = new parse query(parse role); rolequery equalto("name", "administrator"); const adminrole = await rolequery first({ usemasterkey true }); // set class level permissions const schema = new parse schema("post"); // add class level permissions for admin role await schema setclp({ get { " " true }, find { " " true }, create { " " true }, update { "role\ administrator" true, "" true }, // creator and admins can update delete { "role\ administrator" true, "" true }, // creator and admins can delete addfield { "role\ administrator" true } }); await schema update(); console log("admin permissions for post class set successfully"); } catch (error) { console error("error setting admin permissions ", error); throw error; } }; การใช้งาน webhooks back4app ช่วยให้คุณตั้งค่า webhooks เพื่อรวมกับบริการภายนอก // example cloud function that calls an external webhook parse cloud aftersave("post", async (request) => { const post = request object; // only trigger for new posts if (request original) return; try { // get author information const author = post get("author"); await author fetch({ usemasterkey true }); // prepare data for webhook const webhookdata = { postid post id, content post get("content"), authorname author get("username"), createdat post get("createdat") }; // call external webhook (example notify a content moderation service) const response = await parse cloud httprequest({ method 'post', url 'https //your webhook url com/new post', headers { 'content type' 'application/json' }, body webhookdata }); console log("webhook notification sent successfully ", response data); } catch (error) { console error("error sending webhook notification ", error); } }); ขั้นตอนที่ 10 — การปรับแต่งประสิทธิภาพ back4app เมื่อเครือข่ายสังคมของคุณเติบโตขึ้น คุณจะต้องปรับแต่ง backend ของ back4app เพื่อประสิทธิภาพและความสามารถในการขยายตัว ส่วนนี้ครอบคลุมกลยุทธ์ที่สำคัญเพื่อให้แน่ใจว่าแอปพลิเคชันของคุณยังคงรวดเร็วและตอบสนองได้แม้ว่าเบสผู้ใช้ของคุณจะขยายตัวขึ้น การปรับแต่งฐานข้อมูล การออกแบบฐานข้อมูลและการค้นหาที่มีประสิทธิภาพเป็นสิ่งสำคัญสำหรับประสิทธิภาพของแอปพลิเคชัน การสร้างดัชนี ดัชนีช่วยปรับปรุงประสิทธิภาพการค้นหาในฟิลด์ที่ถูกค้นหาบ่อยๆ อย่างมาก ไปที่แดชบอร์ด back4app ของคุณ ไปที่ "database browser" > เลือกคลาส (เช่น post) คลิกที่แท็บ "indexes" สร้างดัชนีสำหรับฟิลด์ที่ถูกค้นหาบ่อยๆ // example creating indexes programmatically const createpostindexes = async () => { try { const schema = new parse schema('post'); // add an index on the author field await schema addindex('author index', { author 1 }); // add an index on createdat for timeline queries await schema addindex('createdat index', { createdat 1 }); // add a compound index for author + createdat await schema addindex('author date index', { author 1, createdat 1 }); await schema update(); console log('indexes created successfully'); } catch (error) { console error('error creating indexes ', error); } }; ฟิลด์สำคัญที่ต้องสร้างดัชนี ผู้เขียน ในคลาส post สำหรับการค้นหาตามผู้ใช้ที่เร็วขึ้น createdat สำหรับการโหลดตามเวลาอย่างรวดเร็ว ชื่อผู้ใช้ ในคลาสผู้ใช้สำหรับการค้นหาผู้ใช้ที่เร็วขึ้น ผู้เข้าร่วม ในคลาสการสนทนาเพื่อการกรองข้อความที่เร็วขึ้น การปรับแต่งคำค้น ปรับแต่งคำค้นของคุณเพื่อลดภาระของเซิร์ฟเวอร์และเวลาในการตอบสนอง // bad fetches all fields and doesn't use limit const fetchuserposts = async (userid) => { const query = new parse query('post'); query equalto('author', userid); const results = await query find(); return results; }; // good only fetches needed fields and uses limit const fetchuserposts = async (userid, page = 0, limit = 20) => { const post = parse object extend('post'); const query = new parse query(post); // get user pointer const userpointer = { type 'pointer', classname ' user', objectid userid }; // only get posts for this user query equalto('author', userpointer); // only select fields we need query select('content', 'image', 'likes', 'createdat'); // sort by date, newest first query descending('createdat'); // implement pagination query limit(limit); query skip(page limit); const results = await query find(); return results; }; การดำเนินการแบบกลุ่ม สำหรับการดำเนินการที่เกี่ยวข้องกับหลายวัตถุ ให้ใช้การดำเนินการแบบกลุ่มเพื่อลดการเรียก api // update multiple posts in a single request const updatemultipleposts = async (postids, updatedata) => { try { const post = parse object extend('post'); const posts = postids map(id => { const post = new post(); post id = id; return post; }); // set the same update data on all posts posts foreach(post => { object entries(updatedata) foreach((\[key, value]) => { post set(key, value); }); }); // save all posts in a single request await parse object saveall(posts); console log('all posts updated successfully'); } catch (error) { console error('error updating posts ', error); throw error; } }; // delete multiple comments in a single request const deletemultiplecomments = async (commentids) => { try { const comment = parse object extend('comment'); const comments = commentids map(id => { const comment = new comment(); comment id = id; return comment; }); // delete all comments in a single request await parse object destroyall(comments); console log('all comments deleted successfully'); } catch (error) { console error('error deleting comments ', error); throw error; } }; การนำการแคชไปใช้ ใช้การแคชฝั่งไคลเอนต์สำหรับข้อมูลที่เข้าถึงบ่อยเพื่อลดการเรียก api // caching user profiles const userprofilecache = new map(); const cache expiry = 10 60 1000; // 10 minutes in milliseconds const getuserprofile = async (userid, forcerefresh = false) => { // check if we have a valid cache entry const now = date now(); const cacheentry = userprofilecache get(userid); if (!forcerefresh && cacheentry && (now cacheentry timestamp < cache expiry)) { console log('using cached user profile'); return cacheentry data; } try { // fetch from back4app const query = new parse query(parse user); const user = await query get(userid); // process user data const userdata = { id user id, username user get('username'), bio user get('bio') || '', avatar user get('avatar') ? user get('avatar') url() null, followers user get('followers') || 0, following user get('following') || 0 }; // store in cache with timestamp userprofilecache set(userid, { data userdata, timestamp now }); return userdata; } catch (error) { console error('error fetching user profile ', error); // return cached data even if expired in case of error if (cacheentry) { console log('using expired cache due to error'); return cacheentry data; } throw error; } }; // clear cache for a user when their profile is updated const invalidateusercache = (userid) => { userprofilecache delete(userid); }; การใช้ livequery อย่างมีประสิทธิภาพ livequery มีความสามารถสูงสำหรับฟีเจอร์เรียลไทม์ แต่ควรใช้ให้มีประสิทธิภาพเพื่อหลีกเลี่ยงปัญหาด้านประสิทธิภาพ // efficient livequery usage const setupefficientlivequery = async (conversationid) => { try { // only subscribe to messages in the active conversation if (!conversationid) return null; // create a focused query const message = parse object extend('message'); const query = new parse query(message); // get the conversation pointer const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; // only get messages for this conversation query equalto('conversation', conversationpointer); // only include necessary fields query select('text', 'sender', 'createdat'); // limit initial results query limit(50); query descending('createdat'); // create subscription const subscription = await query subscribe(); subscription on('create', (message) => { // handle new message console log('new message ', message get('text')); // update ui with new message }); // unsubscribe when switching conversations return subscription; } catch (error) { console error('error setting up livequery ', error); return null; } }; // always unsubscribe when done const cleanuplivequery = (subscription) => { if (subscription) { subscription unsubscribe(); } }; แนวทางปฏิบัติที่ดีที่สุดสำหรับ livequery สมัครสมาชิกเฉพาะข้อมูลที่ต้องการการอัปเดตแบบเรียลไทม์ จำกัดฟิลด์ที่คุณขอด้วย select() ยกเลิกการสมัครเมื่อข้อมูลไม่จำเป็นอีกต่อไป ใช้ข้อจำกัดเพื่อลดขอบเขตของการสมัครสมาชิก พิจารณาการรวม livequery กับการค้นหาปกติเพื่อการโหลดข้อมูลเริ่มต้น การเพิ่มประสิทธิภาพการจัดการไฟล์ การจัดการไฟล์อย่างมีประสิทธิภาพเป็นสิ่งสำคัญสำหรับเครือข่ายสังคมที่มีการอัปโหลดภาพ // resize images before upload to reduce storage and bandwidth const uploadresizedimage = async (originalfile, maxwidth = 1200, maxheight = 1200) => { return new promise((resolve, reject) => { try { const reader = new filereader(); reader onload = (event) => { const img = new image(); img onload = () => { // calculate new dimensions let width = img width; let height = img height; if (width > maxwidth) { height = math round(height (maxwidth / width)); width = maxwidth; } if (height > maxheight) { width = math round(width (maxheight / height)); height = maxheight; } // create canvas for resizing const canvas = document createelement('canvas'); canvas width = width; canvas height = height; // draw resized image const ctx = canvas getcontext('2d'); ctx drawimage(img, 0, 0, width, height); // convert to blob canvas toblob(async (blob) => { const resizedfile = new file(\[blob], originalfile name, { type originalfile type, lastmodified date now() }); // upload to back4app const parsefile = new parse file(resizedfile name, resizedfile); await parsefile save(); resolve(parsefile); }, originalfile type, 0 8); // 0 8 quality for jpeg }; img src = event target result; }; reader readasdataurl(originalfile); } catch (error) { reject(error); } }); }; // use thumbnails for preview images const createthumbnailversion = async (originalfile, thumbnailwidth = 200) => { // similar to above but creating a smaller thumbnail // // return both original and thumbnail return { original originalparsefile, thumbnail thumbnailparsefile }; }; การตรวจสอบแอป back4app ของคุณ back4app มีเครื่องมือหลายอย่างในการตรวจสอบประสิทธิภาพของแอปพลิเคชันของคุณ แดชบอร์ดการวิเคราะห์ ให้สถิติการใช้งานในระดับสูง ตรวจสอบคำขอ api, การใช้พื้นที่เก็บข้อมูล และการดำเนินการไฟล์ ระบุรูปแบบการใช้งานและเวลาที่มีการใช้งานสูงสุด บันทึก ให้ข้อมูลรายละเอียดเกี่ยวกับการดำเนินการ ตรวจสอบบันทึกเซิร์ฟเวอร์สำหรับข้อผิดพลาดและปัญหาประสิทธิภาพ กรองบันทึกตามประเภทเพื่อแยกปัญหา เมตริกประสิทธิภาพ ติดตามเวลาตอบสนองและสุขภาพของระบบ ตรวจสอบประสิทธิภาพของการค้นหา ระบุการดำเนินการที่ช้า กำหนดการแจ้งเตือนเพื่อให้ได้รับการแจ้งเตือนเกี่ยวกับปัญหาที่อาจเกิดขึ้น ไปที่ "การตั้งค่าแอป" > "การแจ้งเตือน" ในแดชบอร์ด back4app ตั้งค่าการแจ้งเตือนสำหรับ การใช้งาน api สูง (ใกล้ขีดจำกัดของแผนของคุณ) อัตราความผิดพลาดพุ่งสูงขึ้น (บ่งชี้ถึงปัญหาของแอปพลิเคชัน) การหยุดทำงานของเซิร์ฟเวอร์ (ส่งผลกระทบต่อผู้ใช้ทั้งหมด) การใช้งานพื้นที่เก็บข้อมูล (ใกล้ขีดความจุ) // implementing custom logging for performance monitoring const logperformance = async (operation, starttime) => { const duration = date now() starttime; // log to back4app const performancelog = parse object extend('performancelog'); const log = new performancelog(); log set('operation', operation); log set('duration', duration); log set('user', parse user current() ? parse user current() id 'anonymous'); log set('timestamp', new date()); await log save(null, { usemasterkey true }); // log locally too console log(`performance ${operation} took ${duration}ms`); }; // example usage const fetchtimelineposts = async () => { const starttime = date now(); try { // query code here // // calculate and log performance await logperformance('fetchtimelineposts', starttime); return results; } catch (error) { console error('error ', error); throw error; } }; กลยุทธ์การขยายตัว เมื่อแอปพลิเคชันของคุณเติบโตขึ้น ให้พิจารณากลยุทธ์การขยายตัวเหล่านี้ อัปเกรดแผน back4app ของคุณ ย้ายไปยังระดับที่สูงขึ้นเมื่อฐานผู้ใช้ของคุณขยายตัว ติดตามการใช้งานและอัปเกรดก่อนที่คุณจะถึงขีดจำกัด ใช้ sharding สำหรับคอลเลกชันขนาดใหญ่ แบ่งข้อมูลออกเป็นหลายคลาสสำหรับชุดข้อมูลที่มีขนาดใหญ่มาก ตัวอย่างเช่น แบ่งข้อความตามปีหรือเดือน // example of sharded messages implementation const sendshardedmessage = async (conversationid, text, sender) => { try { // determine which shard to use (e g , by current month) const now = new date(); const shardname = `message ${now\ getfullyear()} ${now\ getmonth() + 1}`; // create dynamic class reference const messageshard = parse object extend(shardname); const message = new messageshard(); // create a pointer to the conversation const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; // set message data message set('conversation', conversationpointer); message set('text', text); message set('sender', sender); // save to appropriate shard await message save(); return message; } catch (error) { console error('error sending sharded message ', error); throw error; } }; // fetch messages across shards const fetchshardedmessages = async (conversationid, limit = 50) => { try { // get list of all message shards const shards = await getmessageshardnames(); // create a query for each shard const queries = shards map(shardname => { const messageshard = parse object extend(shardname); const query = new parse query(messageshard); // create a pointer to the conversation const conversation = parse object extend('conversation'); const conversationpointer = new conversation(); conversationpointer id = conversationid; query equalto('conversation', conversationpointer); return query; }); // combine queries const mainquery = parse query or( queries); mainquery descending('createdat'); mainquery limit(limit); return await mainquery find(); } catch (error) { console error('error fetching sharded messages ', error); throw error; } }; การใช้งานเซิร์ฟเวอร์แคช ใช้ redis หรือโซลูชันแคชอื่นๆ สำหรับข้อมูลที่เข้าถึงบ่อย ดำเนินการผ่าน cloud code และบริการภายนอก ใช้เครือข่ายการจัดส่งเนื้อหา (cdns) กำหนดค่า cdn เพื่อการส่งไฟล์ที่รวดเร็วขึ้น โดยเฉพาะอย่างยิ่งสำหรับภาพและไฟล์มีเดีย การปรับแต่งความปลอดภัย ความปลอดภัยและประสิทธิภาพมีความสัมพันธ์กันอย่างใกล้ชิด การปรับแต่งเหล่านี้ช่วยปรับปรุงทั้งสองอย่าง ดำเนินการตั้งค่าการอนุญาตชี้ตำแหน่ง ใช้การอนุญาตชี้ตำแหน่งเพื่อจำกัดการเข้าถึงวัตถุที่เกี่ยวข้อง สิ่งนี้ช่วยให้สามารถทำการค้นหาได้อย่างมีประสิทธิภาพมากขึ้นโดยไม่ลดทอนความปลอดภัย // set up pointer permissions const setuppointerpermissions = async () => { try { // get the message class schema const schema = new parse schema('message'); // add pointer permissions only conversation participants can access messages const pointerpermissions = { 'conversation' { 'read' { 'participantsfield' 'participants' } } }; // update the schema with pointer permissions await schema addpointerpermissions(pointerpermissions); await schema update(); console log('pointer permissions set successfully'); } catch (error) { console error('error setting pointer permissions ', error); } }; ใช้ acl ของ parse อย่างมีประสิทธิภาพ acls ให้ความปลอดภัยแต่สามารถส่งผลกระทบต่อประสิทธิภาพหากใช้งานมากเกินไป พิจารณาใช้การอนุญาตระดับคลาสสำหรับกรณีทั่วไปและ acl สำหรับข้อยกเว้น // efficient acl usage set default acl parse setdefaultacl(new parse acl({ publicreadaccess true }), true); // only use specific acls for private content const createprivatepost = async (content, image) => { const currentuser = parse user current(); // create a new private post const post = parse object extend('post'); const post = new post(); // set post data post set('content', content); post set('author', currentuser); if (image) { const parsefile = new parse file(image name, image); await parsefile save(); post set('image', parsefile); } // create private acl const privateacl = new parse acl(currentuser); // only the creator can read and write privateacl setreadaccess(currentuser id, true); privateacl setwriteaccess(currentuser id, true); // set the acl post setacl(privateacl); // save the post await post save(); return post; }; การปรับแต่งเซิร์ฟเวอร์ back4app ปรับแต่งการตั้งค่า parse server ของคุณใน back4app กำหนดค่าพารามิเตอร์เซิร์ฟเวอร์ ไปที่ "การตั้งค่าแอป" > "การตั้งค่าเซิร์ฟเวอร์" ปรับแต่งการตั้งค่า timeout สำหรับการดำเนินการที่ใช้เวลานาน กำหนดค่าขีดจำกัดการร้องขอสูงสุด ตั้งค่าการจำกัดอัตรา ดำเนินการจำกัดอัตราเพื่อป้องกันการละเมิดและรับประกันการใช้งานที่เป็นธรรม กำหนดค่าผ่าน cloud code ตามที่แสดงก่อนหน้านี้ โดยการดำเนินการตามกลยุทธ์การปรับแต่งประสิทธิภาพเหล่านี้ แอปพลิเคชันเครือข่ายสังคมของคุณจะมีความพร้อมในการจัดการการเติบโตและรักษาประสิทธิภาพที่ยอดเยี่ยม แม้ว่าเบสผู้ใช้ของคุณจะขยายตัว อย่าลืมติดตามประสิทธิภาพของแอปพลิเคชันของคุณอย่างต่อเนื่องและทำการปรับปรุงอย่างค่อยเป็นค่อยไปตามที่จำเป็น บทสรุป ในบทแนะนำนี้ คุณได้เรียนรู้วิธีการสร้างแอปพลิเคชันเครือข่ายสังคมที่สมบูรณ์โดยใช้ back4app เป็นบริการแบ็กเอนด์ของคุณ คุณได้ดำเนินการฟีเจอร์หลักเช่น การตรวจสอบผู้ใช้และการจัดการโปรไฟล์ การสร้างโพสต์และการมีปฏิสัมพันธ์ทางสังคม การส่งข้อความแบบเรียลไทม์ด้วย livequery ฟังก์ชันการค้นหา ฟีเจอร์ขั้นสูงเช่น cloud functions และ background jobs parse server ของ back4app ให้โซลูชันแบ็กเอนด์ที่ทรงพลังและสามารถปรับขนาดได้ ซึ่งช่วยให้คุณมุ่งเน้นไปที่การสร้างประสบการณ์ผู้ใช้ที่ยอดเยี่ยมโดยไม่ต้องกังวลเกี่ยวกับโครงสร้างพื้นฐานเซิร์ฟเวอร์ที่ซับซ้อน ขั้นตอนถัดไป เพื่อเพิ่มประสิทธิภาพแอปพลิเคชันเครือข่ายสังคมของคุณให้มากขึ้น พิจารณา การนำการแจ้งเตือนแบบพุชไปใช้ โดยใช้บริการพุชของ back4app การเพิ่มการวิเคราะห์ เพื่อติดตามการมีส่วนร่วมของผู้ใช้ การตั้งค่าตัวเลือกการจัดเก็บไฟล์ สำหรับไฟล์มีเดียขนาดใหญ่ การนำการควบคุมเนื้อหาไปใช้ โดยใช้ cloud functions การสร้างแอปมือถือ โดยใช้ react native กับแบ็กเอนด์เดียวกันของ back4app โดยการใช้ฟีเจอร์ของ back4app และปฏิบัติตามกลยุทธ์การเพิ่มประสิทธิภาพที่ระบุไว้ในบทแนะนำนี้ คุณสามารถสร้างเครือข่ายสังคมที่สามารถขยายเพื่อรองรับผู้ใช้หลายพันคนในขณะที่รักษาประสิทธิภาพสูง แหล่งข้อมูลเพิ่มเติม เพื่อขยายความรู้เกี่ยวกับ back4app ต่อไป นี่คือแหล่งข้อมูลที่มีค่า เอกสาร back4app เอกสาร back4app https //www back4app com/docs/get started/welcome คู่มือ parse javascript https //docs parseplatform org/js/guide/ ช่อง youtube ของ back4app https //www youtube com/c/back4app พาร์สเซิร์ฟเวอร์และไลฟ์ควอรี เอกสารของ parse server https //docs parseplatform org/parse server/guide/ คู่มือ livequery https //docs parseplatform org/parse server/guide/#live queries ความปลอดภัยและการปรับแต่ง คู่มือการอนุญาตระดับคลาส https //docs parseplatform org/rest/guide/#class level permissions คู่มือความปลอดภัย acl https //docs parseplatform org/js/guide/#security การเพิ่มประสิทธิภาพ https //docs parseplatform org/js/guide/#performance ฟีเจอร์ขั้นสูง คู่มือโค้ดคลาวด์ https //docs parseplatform org/cloudcode/guide/ งานเบื้องหลัง https //docs parseplatform org/cloudcode/guide/#cloud jobs การแจ้งเตือนแบบพุช https //docs parseplatform org/js/guide/#push notifications จำไว้ว่าการสร้างเครือข่ายสังคมที่ประสบความสำเร็จเป็นกระบวนการที่ต้องทำซ้ำ เริ่มต้นด้วยพื้นฐานที่มั่นคง (ซึ่งคุณมีอยู่แล้ว) รวบรวมข้อเสนอแนะแบบผู้ใช้ และปรับปรุงแอปพลิเคชันของคุณอย่างต่อเนื่องตามรูปแบบการใช้งานในโลกจริง เราหวังว่าบทแนะนำนี้จะมอบความรู้และความมั่นใจให้คุณในการสร้างแอปพลิเคชันที่น่าทึ่งด้วย back4app ขอให้สนุกกับการเขียนโค้ด!