Giter VIP home page Giter VIP logo

chat-server's Introduction

Socket.IO Server

Server has been deployed to https://server-chat-js.herokuapp.com/

Khởi tạo Server

Tạo SocketIO Server thông qua việc sử dụng thư viện Socket.io, Express và một HTTP Server Cài đặt các package cần thiết

npm i express socket.io uuid 

Cài đặt Sever

const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);

io.on("connection", (socket) => {
  // Some events and communication
});

const PORT = process.env.PORT || 3000;

server.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Một số thao tác với Event

1. Lắng nghe Event

// Listen when user join to room
socket.on("join", ({ name }) => {
  // Something ...
});

// Listen when user send message to room
socket.on("send_message", ({ id, message }) => {
  // Something ...
});

// Listen when user disconnect to Socket.IO Server
socket.on("disconnect", () => {
  // Something ...
});

2. Loại bỏ Event Listener

const listener = (...args) => {
  console.log(args);
}
socket.on("event", listener);

// Remove Event "event"
socket.off("event", listener);

// Remove all events
socket.removeAllListeners();

3. Emit event

// server-side
io.on("connection", (socket) => {
  socket.on("message", (mess) => {
    console.log(mess); // print message
  });
});

// client-side
socket.emit("message", {mess: "Hello world"});

4. Broadcast event

// Send message to all members except sender
io.on("connection", (socket) => {
  socket.broadcast.emit("message", "Send to all member except sender");
});

chat-server's People

Contributors

lethanhvietctt5 avatar

Stargazers

 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.