Skip to content

A web application that allows multiple users to join a chat room and type chat messages by entering a unique username on website load.

License

Notifications You must be signed in to change notification settings

nguy3n47/chat-realtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Members

  1. Lê Hoàng Chương - 18600033
  2. Nguyễn Thị Hương - 18600106
  3. Lê Gia Huy - 18600108
  4. Vũ Cao Nguyên - 18600187

Introduction

What Socket.IO is

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of:

  • a Node.js server: Source | API
  • a Javascript client library for the browser (which can be also run from Node.js): Source | API

How does that work?

The client will try to establish a WebSocket connection if possible, and will fall back on HTTP long polling if not.

WebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser. More information can be found here.

Features

Here are the features provided by Socket.IO over plain WebSockets:

Demo Chat App

Installation

$ npm i --save socket.io

How to use

Sample code:

Server

io.on("connection", (socket) => {
  socket.emit("request" /* … */); // sending to sender-client only
  socket.broadcast.emit("broadcast" /* … */); // sending to all clients except sender
  io.emit("message" /* … */); // sending to all clients, include sender
  socket.on("reply", () => {
    /* … */
  }); // event listener, can be called on client to execute on server
});

Client

<script src="socket.io/socket.io.js"></script>
<script>
  const socket = io("http://localhost:3000");
</script>

Setup

Folder Structure

├── public
│   ├── style.css
│   ├── index.html
│   ├── main.js
├── node_modules
├── index.js
├── README.md
├── package.json
└── .gitignore

package.json

{
  "name": "socket.io-chat",
  "version": "1.0.0",
  "description": "A simple chat client using socket.io",
  "main": "index.js",
  "author": "Nguyen",
  "private": true,
  "license": "MIT",
  "dependencies": {
    "express": "~4.17.1",
    "nodemon": "^2.0.7",
    "socket.io": "^4.0.1"
  },
  "scripts": {
    "start": "nodemon index.js"
  }
}

index.js

// Setup basic express server
const express = require("express");
const app = express();
const path = require("path");
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const port = process.env.PORT || 3000;

// Routing
app.use(express.static(path.join(__dirname, "public")));

// Chat Room
io.on("connection", (socket) => {
  /* … */
});

// Server listening
server.listen(port, () => {
  console.log(`🚀 Server listening at ${port}`);
});

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Socket.IO Chat Example</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <ul class="pages">
      <li class="chat page">
        <div class="chatArea">
          <ul class="messages"></ul>
        </div>
        <input class="inputMessage" placeholder="Type here..." />
      </li>
    </ul>
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script src="/faker.js"></script>
    <script src="/main.js"></script>
  </body>
</html>

Deploying / Testing

https://chat-app-example-socketio.herokuapp.com

License

MIT

About

A web application that allows multiple users to join a chat room and type chat messages by entering a unique username on website load.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published