Uses Socket IO HTTP
HTTP always starts with the request from the browser and ends with the response from the server. This protocol is not suitable for chat, games,…. where communication also has to come from the server.
The Websocket protocol enables communication in both directions on a permanent connection between client and server.▻
The Websocket Protocol
The websockets protocol is based on HTTP and HTTPS:
- it uses ports 80 or 443
- it always starts with a normal HTTP request
But after the first request, the TCP connection is maintained permanently, and the client and server switch to the actual Websocket protocol:
After this first exchange, both the server and the client have to deal with incoming messages at all times.▻
When programming and debugging websockets you need the developer tools: In the “network analysis” you can see the first request, still with HTTP, which is then converted into a websocket with “101 Switching Protocol”:
In the tab "Messages" you can see the messages that are sent back and forth:
Here you can also see that the client and server send each other “ping” packets when nothing else can be sent.▻
Node and the associated package manager are used today not only in backend development, but also very much as a tool for frontend development.
Programming for Node is not easy: as in the browser, asynchronous calls are also used a lot in Node.▻
You can install Node.js on your own computer, but this is not necessary for this example. We use https://glitch.com/:
This eliminates the need to upload the code to a server.▻
Programming the client
There is already an input field for chat messages on the client. All chat messages should be displayed in the list with the id:
Send to the server:
When the form is submitted (by pressing the submit button or by pressing enter), a message is sent to the server:
Received from the server:
When a message comes from the server, it is added to the list as a new one:
Programming the server:
Attention: Node is used to program the entire web server - there is no apache or nginx!
In the following code represents the entire web socket, so it is a broadcast to all connected clients. represents a connected client:▻
There are many ways to further develop the example program:▻
In this example we have only sent and received messages. We can invent any new types of messages, with or without payload.
For example for applause: The client can send an applause message without any further data. The server can send applause with a number▻
Other input and output in the client
Not every input has to come from the text input field: also clicks on buttons, mouse movements, etc. can trigger websocket messages.
Not every issue is a chat message. For example, you could play an audio clip as applause.▻
Different logic on the server
On the server you could count how many users are present:▻
- What is Google Maps Search Optimization
- Are Online Personal Trainers Effective?
- What is the general strategy of Plague Inc.
- How to color confectioner's sugar
- Why is mindfulness meditation so important
- Has anyone visited Glacier 3000
- What is a non-executive founding chairman
- Where is the Pench National Park in India
- Which adventure sport cost the most lives?
- Who is the best teacher of thought
- Who is the most decorated living scientist
- What is the Japanese translation for profound
- Where was the whoopie pie first invented?
- How can I pass 98 381 braindumps
- Serve marines on submarines
- Is paranoia a psychiatric creation
- What is Objectivism in the Bible
- What is a GPG instruction
- Why do people analyze music
- Reduces LiAlH4 alkynes
- Ankit Goyal is also preparing for IES
- Which iOS applications support Google Scholar
- How do addressable RGB LED strips work?
- What is Kamaal R Khan's net worth
- What philosophy does Gregory House live by
- What's your rating of BJP Chhattisgarh
- What do you think of my photo
- Lets you slim down puffed rice
- What happened when Zeus fought Typhon
- What happened to Walter OBrien's sister
- What does Medicare pay for
- What if everything is a simulation
- How do I minimize noise in quadcopters