thelovemsg / chatting_app

0 stars 0 forks source link

Let's know what is web socket and how it works. #17

Open thelovemsg opened 1 year ago

thelovemsg commented 1 year ago

Objective

Let's know what is web socket and how it works.

Detailed work content

I think all the programmers nowadays might have the same thought with me.

"OMG! Am I making logic by myself or chatGPT do this for me?"

I get a lot of help from him and really satisfied with his help. But, problem is that I need to check if I totallly understand what I asked to him.

It's not that different when I tried to use Spring Web Socket.

So, In this issue, I'll make it clear that I need to talk through why web socket is needed, how it works and what functionality I can use.

I'm going to add proper information I still don't know.

Most of the questions are answered by chatGPT. I love you GPT!

Something that I realized

  1. Spring Web Socekt is in-memory message broker that is quite hard to be one size fit all in many cases. So, after i make service with this, I need to introduce another message broker.
thelovemsg commented 1 year ago

What is the meaning of message broker?

A message broker is a software component that facilitates communication between distributed applications, systems, or services by mediating and managing the exchange of messages. It serves as an intermediary between message producers (senders) and message consumers (receivers), allowing them to communicate without needing to know each other's implementation details or locations.

In the context of a Spring Boot WebSocket application, the message broker is responsible for handling and forwarding messages between the clients and the server. When a message is sent from a client, the message broker routes it either to the appropriate @MessageMapping-annotated method (based on the configured application destination prefix) or to other clients that have subscribed to the matching destination (based on the topic or queue prefix).

The primary functions of a message broker include:

  1. Routing Ensuring messages are delivered to the appropriate destination or consumers, based on predefined routing rules or message patterns.
  2. Decoupling Allowing message producers and consumers to communicate without needing to know each other's details or locations, promoting loose coupling and easier maintenance.
  3. Message transformation Converting messages from one format to another, enabling seamless communication between systems with different data formats or protocols.
  4. Message persistence Optionally storing messages for a certain period, ensuring they can be delivered even if the consumer is not available at the time of message production.

In the case of the Spring Boot WebSocket application, the message broker can be an in-memory simple broker or a more advanced broker like RabbitMQ or Apache Kafka. The choice of the message broker depends on your application requirements and the desired level of features, scalability, and reliability.

thelovemsg commented 1 year ago

Is it fine to use Spring web socket all the time?

Using Spring WebSocket can still be a suitable choice for a scalable chat application, but you may need to consider additional factors to ensure it meets your performance and scalability requirements. The built-in simple in-memory message broker is not suitable for a large-scale application, as it doesn't support clustering and has limited features.

For a scalable chat application using Spring WebSocket, you may consider the following:

  1. Use an external message broker Replace the simple in-memory message broker with a more robust and scalable external message broker, such as RabbitMQ or Apache Kafka. These message brokers provide better performance, clustering support, and advanced features that can handle higher message throughput and larger numbers of clients.

  2. Load balancing Deploy multiple instances of your Spring Boot WebSocket application behind a load balancer to distribute the load across multiple servers. Make sure to use a load balancer that supports WebSocket connections, such as NGINX or HAProxy.

  3. Session replication In a clustered environment, you may need to share user session data across multiple instances of your application. You can use solutions like Spring Session with Redis or another distributed data store to manage session data in a scalable manner.

  4. Optimize message processing Ensure your application processes messages efficiently by using asynchronous processing, proper data structures, and caching where appropriate. Optimize database access and consider using a NoSQL database like MongoDB or Cassandra if it better suits your data model and access patterns.

  5. Monitor and tune performance Regularly monitor your application's performance using tools like Micrometer or Spring Boot Actuator, and tune the performance based on the gathered metrics.

By considering these factors and making the appropriate architectural decisions, you can build a scalable chat application using Spring WebSocket. However, the specific choice of technology depends on your requirements and constraints, so it's important to evaluate and compare different options to find the best solution for your use case.

thelovemsg commented 1 year ago

Why method 'config.enableSimpleBroker' need point-to-point message?

The config.enableSimpleBroker() method is used to enable a simple, in-memory message broker for your Spring Boot WebSocket application. The broker is responsible for managing the exchange of messages between clients and the server. When you pass destination prefixes like /topic and /queue to this method, you are configuring the broker to handle different types of messaging patterns.

The reason for supporting point-to-point messaging (using the /queue prefix) in addition to broadcasting (using the /topic prefix) is to provide flexibility in how messages are delivered to clients. Depending on your application's requirements, you might need to send messages to specific clients or to all clients subscribed to a particular destination.

Here's a brief overview of the two messaging patterns:

Broadcasting (pub/sub) pattern: Messages sent to a destination with the /topic prefix are meant for all clients that have subscribed to that destination. This is useful for cases where you want to send a message to multiple clients simultaneously, such as chat rooms, notifications, or real-time updates.

Point-to-point (queue) pattern: Messages sent to a destination with the /queue prefix are meant for a specific client or user session. The broker ensures that the message is delivered to the intended recipient only. This is useful for cases where you want to send a private message to a specific user or handle communication between two users in a one-on-one conversation.

By supporting both messaging patterns, the simple broker provides the flexibility to handle different types of communication scenarios in your application. If you need more advanced features, scalability, or reliability, you can consider using an external message broker like RabbitMQ or Apache Kafka instead of the simple in-memory broker.

thelovemsg commented 1 year ago

What I understand when using Spring Web Socket.

  1. @MessageMapping just help to get message from client but @SendTo can handle which one will take message.
  2. @SendTo is used to specify the destination to which the message should be sent after it has been processed. The server will send the processed message to all clients that are subscribed to the specified destination.
thelovemsg commented 1 year ago

The choice between an in-memory message broker

The choice between an in-memory message broker, RabbitMQ, or Apache Kafka really depends on your specific use case. Here are some examples:

In-Memory Message Broker

An in-memory message broker like the one provided by Spring Boot is lightweight, easy to set up, and has low latency because everything is in-memory.

Example use-case: You are building a prototype or a small-scale application where you need real-time communication, but the message volume isn't huge, and you don't need advanced features like message routing, durability, or replication. The in-memory message broker is also a good fit for testing environments.

RabbitMQ

RabbitMQ is a robust, easy-to-use, and flexible message broker that supports multiple messaging protocols. It offers features like message durability, routing, clustering, and high availability.

Example use-case: You are building a medium-scale application where you need more advanced features like message routing and durable messages. For instance, if you're building a chat application that needs to route messages to specific users or rooms, RabbitMQ's routing capabilities would be handy. Also, if it's critical not to lose any messages even when your application crashes, you'd benefit from RabbitMQ's message durability feature.

Apache Kafka

Apache Kafka is designed for high-throughput, fault-tolerant, distributed messaging. It's well-suited for real-time streaming data and can handle huge volumes of messages. Kafka also provides features like message replication for fault tolerance, partitioning for scalability, and message replay capabilities.

Example use-case: You are building a large-scale application that needs to handle a huge volume of messages or real-time streaming data. For instance, if you're building a real-time analytics platform that ingests, processes, and analyzes millions of events per second, Kafka would be a great choice because of its high-throughput capabilities. Kafka's message replay capabilities would also be beneficial if your analytics platform needs to reprocess events from the past.

In general, the choice of the message broker should match your application's scale and requirements. Starting with an in-memory broker might be a good choice for simplicity, and you can switch to a more advanced broker like RabbitMQ or Kafka as your application grows and your needs become more complex.

thelovemsg commented 1 year ago

How web socket works?

WebSockets is a communication protocol which provides full-duplex communication channels over a single TCP connection. This is particularly useful for real-time, event-driven web applications. Here's how WebSockets generally work:

Client-Server Handshake:

The process begins with the client (typically a web browser) sending an HTTP request to a server to initiate a WebSocket connection. This request is known as a WebSocket handshake request. It looks like a standard HTTP request, but it includes an "Upgrade: websocket" header to inform the server that the client wishes to establish a WebSocket connection.

Server Response:

The server, if it supports the WebSocket protocol, will agree to the upgrade and communicate its agreement by sending back an HTTP response with "101 Switching Protocols" status code. The response also contains a header "Connection: Upgrade" and "Upgrade: websocket", indicating the protocol switch.

Connection Established:

After a successful handshake, the protocol switches from HTTP to WebSocket, and a persistent connection is established between the client and the server. This connection remains open until it is explicitly closed by either the client or the server. The connection allows for bidirectional communication, meaning that both the client and the server can send data at any time.

Data Frames:

Once the connection is established, the client and the server can send messages back and forth. Each piece of data sent over the WebSocket connection is referred to as a 'message', and messages are made up of one or more 'frames'. The WebSocket protocol defines the structure of these frames, including how to indicate the type of data being transported (text, binary data, close connection, etc.)

Data Exchange:

Clients and servers can now exchange data in real-time. This could be anything from chat messages in a real-time chat application, live updates in a sports app, real-time trading data in a stock trading application, and so forth.

Closing the Connection:

Either the client or the server can decide to close the WebSocket connection at any time. This is done by sending a special type of data frame known as a 'close frame'. The receiving party can then send back a close frame to acknowledge the closure, and finally, the TCP connection is terminated.

It's important to note that while WebSockets start with an HTTP handshake, they are not restricted by the same rules and limitations of HTTP once the connection is established. This makes them very powerful for certain types of web applications, particularly those requiring real-time, bidirectional communication.

thelovemsg commented 1 year ago

Describe a typical flow diagram for WebSocket communication:

  1. A client (usually a web browser) sends a WebSocket handshake request to a server using the HTTP protocol. This request includes specific headers indicating that it wants to upgrade the connection to a WebSocket.

  2. The server, if it supports the WebSocket protocol, responds with an HTTP 101 status code (Switching Protocols) along with the appropriate headers to confirm that it's switching to the WebSocket protocol.

  3. Now, a full-duplex WebSocket connection is established between the client and the server. They can send and receive messages at any time. This is different from HTTP where the client makes a request and waits for the server to respond.

  4. The WebSocket connection stays open until either the client or the server sends a close frame and closes the connection.

image

thelovemsg commented 1 year ago

Polling vs Long Polliing vs Web Socket

Polling

The client sends a request to the server at regular intervals, asking for any new information. The server responds with the data if it's available or an empty response if it's not. While this method is simple, it's also very inefficient because many requests return empty responses, especially when new data is infrequent. This leads to wasted resources on both the client and server side.

Long Polling

When the client sends a request, instead of immediately responding with no new data, the server holds onto the request until new data is available or a timeout occurs. Once the client gets a response, it immediately sends another request. This reduces the number of empty responses and makes better use of resources, but it still involves a lot of requests back and forth.

Web Socket

This is a more modern technology that provides a full-duplex, persistent connection between the client and server. Once the WebSocket connection is established, data can be sent back and forth as needed, with no need for the constant request-response cycle of polling or long polling. This allows for real-time data updates and is far more efficient. However, it's more complex to set up and requires more modern browsers and servers.

In summary, if you're dealing with real-time data and you have the resources to implement it, WebSockets is usually the best choice. If that's not an option, long polling is a more efficient alternative to regular polling. Regular polling may still be used in certain circumstances, such as when you're dealing with older systems that don't support the other techniques, but it's generally the least efficient option.

thelovemsg commented 1 year ago

STOMP, Socket JS, Web Socket? Overall Process of them with Spring Web Socket

Spring WebSocket is a module in Spring Framework that allows you to build WebSocket-based applications. It provides the necessary tools to manage WebSocket connections and to send and receive messages. On the front-end, you might use a library such as SockJS along with a message protocol like STOMP.

When you use Spring WebSocket with STOMP as your messaging protocol and SockJS for fallback options, your application would typically follow a flow like this:

  1. Client Connection The client (React application) attempts to establish a WebSocket connection to the server. The connection request is sent to a specific endpoint on the server (like "/ws").

  2. SockJS Fallback If WebSockets are not available due to browser or network issues, SockJS will attempt to fall back to another communication option that is available, such as long polling or server-sent events.

  3. Spring Handles Connection On the server-side, the Spring application has a WebSocket configuration that is ready to accept this connection. It establishes the connection and maintains it for as long as the client stays connected.

  4. STOMP over WebSocket Once the connection is established, the client can send STOMP messages over the WebSocket connection. STOMP messages could include a subscription to a topic (SUBSCRIBE), or a message to be sent to a destination (SEND).

  5. Spring Handles STOMP Messages On the server-side, Spring routes these messages based on their destination. For instance, if a message is sent to "/topic/news", it will be routed to all clients that are subscribed to this topic.

  6. Receiving Messages When the server has a message to send to the client (either because it was sent to a topic the client is subscribed to, or it's a direct message to the client), it sends a STOMP message over the WebSocket connection to the client.

  7. Client Handles Message The client receives the STOMP message and handles it accordingly, updating the UI if necessary.

This setup allows your React and Spring applications to communicate in real-time, with STOMP making it easier to handle different types of messages (like connecting, disconnecting, subscribing to a topic, or sending a message), and SockJS ensuring that the application works on all browsers and networks.