Anuj-Panthri commited on
Commit
e4ce9c9
·
1 Parent(s): f1b8b13

added message time at server end

Browse files
client/src/components/ChatFooter/ChatFooter.jsx CHANGED
@@ -10,27 +10,26 @@ function ChatFooter({
10
  const socket = useContext(SocketContext);
11
  const messageRef = useRef();
12
 
13
-
14
 
15
- useEffect(()=>{
16
- if(messageRef.current){
 
17
  messageRef.current.focus();
18
  }
19
  }) // risky this running all the time
20
 
21
  const sendMessage = (message) => {
22
 
23
- if(message.trim()==="") return;
 
24
 
25
- const curr_time = Math.floor(new Date().getTime() / 1000);
26
- const message_bundle= {
27
  message,
28
- send_at:curr_time,
29
- send_to:currentChatId,
30
  }
31
 
32
- if(socket){
33
- socket.emit("send_message",message_bundle);
34
  }
35
 
36
  messageRef.current.value = "";
 
10
  const socket = useContext(SocketContext);
11
  const messageRef = useRef();
12
 
 
13
 
14
+
15
+ useEffect(() => {
16
+ if (messageRef.current) {
17
  messageRef.current.focus();
18
  }
19
  }) // risky this running all the time
20
 
21
  const sendMessage = (message) => {
22
 
23
+ if (message.trim() === "") return;
24
+
25
 
26
+ const message_bundle = {
 
27
  message,
28
+ send_to: currentChatId,
 
29
  }
30
 
31
+ if (socket) {
32
+ socket.emit("send_message", message_bundle);
33
  }
34
 
35
  messageRef.current.value = "";
server/index.ts CHANGED
@@ -338,6 +338,7 @@ io.on("connection", async (socket) => {
338
  // an event was received from the client
339
 
340
  const msg_id = generateId(15);
 
341
 
342
  const stmt = db.prepare("insert into messages(id,message,send_to,send_from,send_at) values(?,?,?,?,?)");
343
 
@@ -346,7 +347,7 @@ io.on("connection", async (socket) => {
346
  data.message,
347
  data.send_to,
348
  socket.data.user.id,
349
- data.send_at,
350
  );
351
 
352
  console.log("saved message:", data.message);
@@ -354,13 +355,14 @@ io.on("connection", async (socket) => {
354
  const sockets = await io.fetchSockets();
355
  const message_bundle = {
356
  ...data,
357
- "id":msg_id,
358
  "send_to": data.send_to,
359
  "send_from": socket.data.user.id,
 
360
  "is_seen": 0,
361
  };
362
 
363
- socket.emit("receive_message",message_bundle);
364
  sockets.forEach((client) => {
365
  if (client.data.user.id == data.send_to) {
366
  // console.log("heyy");
@@ -370,11 +372,11 @@ io.on("connection", async (socket) => {
370
 
371
  });
372
 
373
- socket.on("get_contact",async contact_id=>{
374
  const contact = await db.prepare("select * from users where id = ?").get(contact_id);
375
-
376
 
377
- socket.emit("contact",{
 
378
  ...contact,
379
  last_message: await getLastMessage({ user_id: socket.data.user.id, contact_id: contact_id }),
380
  });
 
338
  // an event was received from the client
339
 
340
  const msg_id = generateId(15);
341
+ const send_at = Math.floor(new Date().getTime() / 1000);
342
 
343
  const stmt = db.prepare("insert into messages(id,message,send_to,send_from,send_at) values(?,?,?,?,?)");
344
 
 
347
  data.message,
348
  data.send_to,
349
  socket.data.user.id,
350
+ send_at,
351
  );
352
 
353
  console.log("saved message:", data.message);
 
355
  const sockets = await io.fetchSockets();
356
  const message_bundle = {
357
  ...data,
358
+ "id": msg_id,
359
  "send_to": data.send_to,
360
  "send_from": socket.data.user.id,
361
+ "send_at": send_at,
362
  "is_seen": 0,
363
  };
364
 
365
+ socket.emit("receive_message", message_bundle);
366
  sockets.forEach((client) => {
367
  if (client.data.user.id == data.send_to) {
368
  // console.log("heyy");
 
372
 
373
  });
374
 
375
+ socket.on("get_contact", async contact_id => {
376
  const contact = await db.prepare("select * from users where id = ?").get(contact_id);
 
377
 
378
+
379
+ socket.emit("contact", {
380
  ...contact,
381
  last_message: await getLastMessage({ user_id: socket.data.user.id, contact_id: contact_id }),
382
  });