thelovemsg / chatting_app

0 stars 0 forks source link

Make real chatting functionality with others in chatting room. #16

Open thelovemsg opened 1 year ago

thelovemsg commented 1 year ago

Objective

Make real chatting functionality with others in chatting room.

Detailed work content

  • I need to make sure that chatting app really can help people to talk each other.

    Note

    Work process

    1. User enter the existing chatting room.
    2. If he've been entering the chatting room, He'll have no problem to chat again. But, he need to upload all the chatting he didn't read.
    3. If user enter the room, room show the message 'somebody entered the room'
    4. He can see which message is sent by me or not. Each messages must be arranged left for other's message and right for my messgae.
    5. If someone in the room don't read the message, message count still remains.
    6. At the top of the chatting room, we can see how many of users entered this room. If someone exit the room, all the user will know who left the room and number decreased.
    7. Messages must be sent in real-time so that all the people can know it right away.
    8. When user enter the room again and read the last messages, he can read it just in case message's send date doesn't exceed over one month.
    9. If user exit the room, he can't get any message from the room until he get invidation again.

Problem

  1. how can I make them to read the data, the messages, if he come back to the room? Is is really just the real-tiem problem?
  2. I think using Redis would be a great choice. We need to study this!
  3. Web socket is still little bit confusing for me. We need to talk through this again!

More problems will be updated.

thelovemsg commented 1 year ago

I'm making chatting app and I want to send files to others while chatting

In the context of a chat application where you want to send files while maintaining a chat session, you might want to consider the following approach:

Combining WebSocket (or libraries like Socket.IO or SockJS) with HTTP/HTTPS

WebSockets are great for real-time, bidirectional communication like chat messages. However, for file transfer, especially for larger files, HTTP/HTTPS could be more suitable. Here's a potential workflow:

  1. Chat messages Use WebSockets (or libraries like Socket.IO or SockJS) to handle real-time chat functionality.

  2. File transfer When a user wants to send a file, use an HTTP/HTTPS POST request to upload the file to the server.

  3. File storage and retrieval Upon receiving a file, the server could store it in a suitable storage system. This could be the server's filesystem, a database (though this may not be ideal for larger files), or a cloud storage service like Amazon S3, Google Cloud Storage, or Azure Blob Storage. The server would then send a message through the WebSocket connection to the other chat participants with a URL for downloading the file.

  4. Downloading the file When a user wants to download the file, their client can simply make an HTTP/HTTPS GET request to the provided URL.

This method separates the concerns of real-time communication and file transfer, allowing each to be handled in a way that plays to its strengths. WebSocket keeps the chat responsive and real-time, and HTTP/HTTPS handles the file transfer in a reliable and scalable way.

Remember, if you're handling user-uploaded files, it's important to consider security implications, such as validating the type and size of files and sanitizing file metadata to prevent potential attacks.

thelovemsg commented 1 year ago

The reason we need a different technique for sending and receiving files

The reason we need a different technique for sending and receiving files, especially large ones, is due to how different protocols are designed and optimized for different kinds of data and communication patterns. Here's a more detailed explanation:

1. Real-time Communication

Real-time applications such as chat systems require a constant, open connection for quick exchange of small amounts of data. WebSockets, Socket.IO, and SockJS are designed for this purpose. They are very efficient at maintaining an open connection and enabling the quick, real-time exchange of small messages in both directions between client and server. However, they're not designed to handle large data transfers efficiently.

2. Large Data Transfer

Sending a large file over a WebSocket connection can saturate the connection, potentially causing delays in the real-time communication happening on the same connection. Furthermore, WebSocket, and similar technologies, are not designed to handle the complexity of large file transfers, such as managing partial transfers, resuming interrupted transfers, error checking, and other robustness features that are important when dealing with large files.

3. HTTP/HTTPS for File Transfer

HTTP and HTTPS, on the other hand, are designed for reliable transfer of larger amounts of data. They have features to handle large data transfers, including status codes to confirm successful transfer, support for partial content delivery, and built-in support for multi-part form data (useful for file upload forms). HTTP/HTTPS connections are stateless and are closed after each transfer, making them less suitable for real-time communication, but more efficient and robust for one-off, larger data transfers such as file uploads and downloads.

4. Scalability and Efficiency

By separating real-time communication and file transfer, each can be scaled independently. If your application needs to support larger files or more file transfers, you can scale up your file handling infrastructure without impacting your real-time messaging service. Likewise, if your real-time messaging needs to support more users or more messages, you can scale that independently of your file handling service.

Therefore, using a combination of protocols helps to provide the best user experience, balancing real-time interactivity with robust and efficient file transfer.

thelovemsg commented 1 year ago

I need to know the total number of users.

When a user enters a room, you'd add them to the room's user set and save that to the database. Similarly, when a user leaves a room, you'd remove them from the room's user set and save the change to the database.

To get the number of users in a room, you'd fetch the ChatRoom entity from the database and call chatRoom.getUsers().size(). This approach lets you get the count of users without needing to create a separate entity or column for it; the count is a derived property of the ChatRoom entity.

Keep in mind that this is a simplified example and a real-world application might need additional features and considerations. For example, if your chat room can have a large number of users, you might want to avoid fetching all users just to get the count, and instead use a query that counts the users on the database side. Additionally, this example assumes that each user can be in only one room at a time; if users can join multiple rooms, you'd need to adjust the associations accordingly.