Walkie-Talkie Style Chat Interface with HTML, CSS, and JavaScript

WHAT TO KNOW - Sep 8 - - Dev Community

<!DOCTYPE html>



Walkie-Talkie Style Chat Interface with HTML, CSS, and JavaScript

<br> body {<br> font-family: Arial, sans-serif;<br> margin: 0;<br> padding: 0;<br> background-color: #f4f4f4;<br> }</p> <p>.container {<br> display: flex;<br> height: 100vh;<br> align-items: center;<br> justify-content: center;<br> }</p> <p>.chat-window {<br> width: 400px;<br> background-color: #fff;<br> border-radius: 10px;<br> box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);<br> padding: 20px;<br> }</p> <p>.chat-header {<br> display: flex;<br> justify-content: space-between;<br> align-items: center;<br> margin-bottom: 20px;<br> }</p> <p>.chat-header h2 {<br> margin: 0;<br> }</p> <p>.chat-header .close-button {<br> cursor: pointer;<br> font-size: 20px;<br> color: #ccc;<br> }</p> <p>.chat-body {<br> height: 300px;<br> overflow-y: auto;<br> padding: 10px;<br> }</p> <p>.message {<br> padding: 10px;<br> margin-bottom: 10px;<br> border-radius: 5px;<br> }</p> <p>.message.sent {<br> background-color: #e0f2f7;<br> text-align: right;<br> }</p> <p>.message.received {<br> background-color: #f0f0f0;<br> text-align: left;<br> }</p> <p>.chat-input {<br> display: flex;<br> align-items: center;<br> margin-top: 20px;<br> }</p> <p>.chat-input input {<br> flex: 1;<br> padding: 10px;<br> border: 1px solid #ccc;<br> border-radius: 5px;<br> }</p> <p>.chat-input button {<br> padding: 10px 20px;<br> background-color: #3498db;<br> color: #fff;<br> border: none;<br> border-radius: 5px;<br> cursor: pointer;<br> }<br>






Walkie-Talkie Chat







<!-- Messages will be displayed here -->




Send




<br> const chatBody = document.querySelector(&#39;.chat-body&#39;);<br> const inputField = document.querySelector(&#39;.chat-input input&#39;);<br> const sendButton = document.querySelector(&#39;.chat-input button&#39;);<br> const closeButton = document.querySelector(&#39;.chat-header .close-button&#39;);</p> <p>sendButton.addEventListener(&#39;click&#39;, sendMessage);<br> inputField.addEventListener(&#39;keyup&#39;, (event) =&gt; {<br> if (event.key === &#39;Enter&#39;) {<br> sendMessage();<br> }<br> });<br> closeButton.addEventListener(&#39;click&#39;, closeChat);</p> <p>function sendMessage() {<br> const message = inputField.value.trim();<br> if (message !== &#39;&#39;) {<br> const messageElement = document.createElement(&#39;div&#39;);<br> messageElement.classList.add(&#39;message&#39;, &#39;sent&#39;);<br> messageElement.textContent = message;<br> chatBody.appendChild(messageElement);</p> <div class="highlight"><pre class="highlight plaintext"><code>inputField.value = ''; chatBody.scrollTop = chatBody.scrollHeight; </code></pre></div> <p>}<br> }</p> <p>function closeChat() {<br> document.querySelector(&#39;.chat-window&#39;).style.display = &#39;none&#39;;<br> }<br>


Walkie-Talkie Style Chat Interface with HTML, CSS, and JavaScript



Imagine a communication interface that mimics the intuitive and instant nature of a walkie-talkie, where messages are sent and received in real-time with a simple press of a button. That's the essence of a walkie-talkie style chat interface, and in this article, we'll explore how to build one using HTML, CSS, and JavaScript.



This type of interface can be particularly beneficial for:



  • Instantaneous Communication:
    Perfect for situations where quick and direct communication is vital, like team collaborations, emergency responses, or even gaming.

  • User-Friendly Interface:
    The simplicity of a walkie-talkie interface makes it easily accessible to users of all technical levels.

  • Enhanced Engagement:
    The real-time nature of the interface can create a more immersive and engaging user experience.


Building the Interface


Let's break down the creation of this walkie-talkie style chat into steps, providing you with code examples along the way.

  1. HTML Structure

  <!DOCTYPE html>
  <html>
   <head>
    <title>
     Walkie-Talkie Chat
    </title>
    <link href="style.css" rel="stylesheet"/>
   </head>
   <body>
    <div class="container">
     <div class="chat-window">
      <div class="chat-header">
       <h2>
        Walkie-Talkie Chat
       </h2>
       <span class="close-button"></span>
      </div>
      <div class="chat-body">
       <!-- Messages will be displayed here -->
      </div>
      <div class="chat-input">
       <input placeholder="Type your message..." type="text"/>
       <button>
        Send
       </button>
      </div>
     </div>
    </div>
    <script src="script.js">
    </script>
   </body>
  </html>
  • container: This div acts as the main container for the entire chat window.
  • chat-window: This div encloses the chat interface itself.
  • chat-header: This div holds the title of the chat and a close button.
  • chat-body: This div will display the messages sent and received.
  • chat-input: This div houses the input field for typing messages and a send button.

    1. CSS Styling

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #f4f4f4;
}

.container {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
}

.chat-window {
  width: 400px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.chat-header h2 {
  margin: 0;
}

.chat-header .close-button {
  cursor: pointer;
  font-size: 20px;
  color: #ccc;
}

.chat-body {
  height: 300px;
  overflow-y: auto;
  padding: 10px;
}

.message {
  padding: 10px;
  margin-bottom: 10px;
  border-radius: 5px;
}

.message.sent {
  background-color: #e0f2f7;
  text-align: right;
}

.message.received {
  background-color: #f0f0f0;
  text-align: left;
}

.chat-input {
  display: flex;
  align-items: center;
  margin-top: 20px;
}

.chat-input input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.chat-input button {
  padding: 10px 20px;
  background-color: #3498db;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

This CSS styles the chat window to give it a clean and modern look. It includes styling for the header, body, input field, send button, and individual messages, creating a visual representation of a walkie-talkie chat.

  1. JavaScript Functionality

const chatBody = document.querySelector('.chat-body');
const inputField = document.querySelector('.chat-input input');
const sendButton = document.querySelector('.chat-input button');
const closeButton = document.querySelector('.chat-header .close-button');

sendButton.addEventListener('click', sendMessage);
inputField.addEventListener('keyup', (event) =&gt; {
  if (event.key === 'Enter') {
    sendMessage();
  }
});
closeButton.addEventListener('click', closeChat);

function sendMessage() {
  const message = inputField.value.trim();
  if (message !== '') {
    const messageElement = document.createElement('div');
    messageElement.classList.add('message', 'sent');
    messageElement.textContent = message;
    chatBody.appendChild(messageElement);

    inputField.value = '';
    chatBody.scrollTop = chatBody.scrollHeight;
  }
}

function closeChat() {
  document.querySelector('.chat-window').style.display = 'none';
}

This JavaScript code handles the core logic of the chat interface:

  • Selecting Elements: It selects the necessary HTML elements for interaction, like the chat body, input field, send button, and close button.
  • Event Listeners: Event listeners are attached to the send button and input field to capture user actions (clicking the button and pressing Enter).
  • sendMessage(): This function is triggered when the send button is clicked or Enter is pressed. It grabs the message from the input field, creates a new message element, adds it to the chat body, and clears the input field.
  • closeChat(): This function hides the chat window when the close button is clicked.

    Adding More Features

    To enhance the walkie-talkie chat experience, we can add more features like:

    1. Audio Feedback

  • "Squelch" Sound: Play a short audio clip when the "Send" button is pressed, similar to the sound of a walkie-talkie squelch.
  • Incoming Message Sound: Play a different audio clip when a message is received from the other party.

    1. User Names

  • Display User Names: Add a user name next to each message to distinguish between different users. You can achieve this by:
  • Using a dropdown to select usernames at the beginning.
  • Storing usernames in local storage or a database for persistence.

  • Multiple Users
  • Real-time Communication: For a true walkie-talkie experience, you'll need a real-time communication backend, such as:
  • WebSockets: A WebSocket connection allows for continuous communication between the client and server.
  • Firebase Realtime Database: A cloud-based database that allows you to synchronize data between users in real-time.

  • User Avatars
  • Display User Avatars: Add profile pictures or avatars next to each user's messages to personalize the experience.

  • Message Styling
  • Different Message Colors: Use different colors for messages sent and received to visually distinguish them.
  • Message Time Stamps: Display the time of each message next to it for chronological reference.

    1. Voice Recognition

  • Voice Input: Implement voice recognition using JavaScript's Web Speech API to allow users to speak their messages instead of typing.

    Conclusion

    Building a walkie-talkie style chat interface using HTML, CSS, and JavaScript can be a fun and rewarding project. This interface offers a unique user experience, emphasizing instant communication and simplicity. The core elements of the interface are fairly straightforward to implement, and the possibilities for adding features and enhancing functionality are extensive.

Best Practices:

  • Clean Code: Use proper HTML structure, clear CSS classes, and organized JavaScript functions for maintainability.
  • Responsive Design: Ensure that the interface adapts well to different screen sizes for optimal user experience on various devices.
  • Accessibility: Consider accessibility standards for color contrast, font sizes, and keyboard navigation.
  • Security: If handling user data, implement appropriate security measures to protect sensitive information.

Further Exploration:

  • Real-time Communication: Explore WebSocket protocols or cloud-based services like Firebase for real-time communication between multiple users.
  • Advanced UI/UX: Experiment with animation, sound effects, and custom styling to create a more engaging and polished user interface.
  • Mobile Applications: Consider developing a native mobile app for enhanced performance and device features.

By experimenting with the code and features described in this article, you can create a highly engaging and functional walkie-talkie style chat interface, bringing the intuitive communication style of walkie-talkies to your web applications.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player