Giter VIP home page Giter VIP logo

tushar-prabhu / flutter-chat-app Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 288 KB

A Flutter Chat Application, with node js backend using WebSocket, messages stored in MongoDB (local, not fully functional).

Kotlin 0.21% Swift 2.93% Objective-C 0.06% Dart 15.53% CMake 31.80% C++ 39.09% C 2.42% HTML 3.13% JavaScript 4.83%
expressjs flutter flutter-android node websocket-chat flutter-web flutter-windows dart mongodb-compass

flutter-chat-app's Introduction

Flutter Chat App with WebSocket

This Flutter application and Node.js backend server demonstrate a simple real-time chat application using WebSocket communication.

Features

  • Real-time messaging using WebSocket.
  • Simple and intuitive user interface.
  • Visual indication of connection status in the app bar.
  • Ability to send and receive messages.

Prerequisites

Flutter App

Before running the app, make sure you have the following:

  • Flutter installed on your development environment.
  • A WebSocket server running. You can use a local server for testing.

Node.js Backend

Before running the backend server, ensure you have the following:

  • Node.js installed on your server.

  • The required npm packages installed. You can install them by running:

    npm install express moment ws

Getting Started

Flutter App

  1. Navigate to the project directory:

    cd frontend
  2. Install dependencies:

    flutter pub get
  3. Open the project in your preferred Flutter development environment.

  4. Update the WebSocket server URL:

    Open the channelconnect method in the ChatPageState class, and modify the WebSocket server URL:

    channel = IOWebSocketChannel.connect("ws://your-server-ip:your-server-port/$myid");
  5. Run the app on two devices or simulators/emulators.

Node.js Backend

  1. Navigate to the project directory:

    cd backend
  2. Install dependencies:

    npm install
  3. Start the server:

    node server.js
  4. The server will be running at http://localhost:8000. You can customize the port in the code.

Usage

  1. Launch the Flutter app on two devices or simulators/emulators.

  2. Each instance of the app can act as a sender or receiver. Update the myid and receiverid variables in the ChatPageState class accordingly.

  3. Messages sent from one instance will be received by the other, creating a chat experience.

Code Structure

  • ChatPage: The main widget for the chat page.
  • ChatPageState: Manages the state of the ChatPage widget, including WebSocket connection and message handling.
  • MessageData: Represents the model for chat messages.

WebSocket Server

  • The WebSocket server is initiated using the ws library on port 6060.
  • It listens for incoming connections and assigns a unique user ID based on the URL path.

WebSocket Events

Connection

  • When a WebSocket connection is established, the server logs the user ID and adds the connection to the webSockets object.

Message Reception

  • The server listens for incoming messages from clients.
  • It validates the message format and authentication key.
  • If authentication is successful and the message is of the "send" type, it forwards the message to the recipient's WebSocket connection.

Disconnection

  • When a WebSocket connection is closed, the server removes the corresponding entry from the webSockets object.

Customization

  • Change the port number in the const port = 8000; line to the desired port for HTTP connections.
  • Modify the WebSocket server port in const wss = new WebSocket.Server({ port: 6060 }); as needed.

flutter-chat-app's People

Contributors

tushar-prabhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.