Giter Site home page Giter Site logo

react-socket-chat's Introduction

React Socket Chat

This is a simple example of a chat created with React and websockets.

The master branch contains the code without the sockets implementation. If you switch to addSocket branch, you will see the code with the sockets already added.

Usage

  • In server folder, run npm run dev in order to start it. It will run in port 5000 by default.
  • In client folder, run npm start to start the React app.

More info about sockets: https://socket.io/docs/

Here are some of the steps done to implement sockets:

Server

  1. Install socket.io package.

  2. Require it on bin/www:

    const io = require('socket.io').listen(server);
  3. Listen for connections:

    io.on("connection", socket => {
    //here go the events
    })
  4. Receive messages from client and relay them to the rest of the clients:

    socket.on("userConnect", user =>{
      console.log(`${user} connected`)
    	socket.broadcast.emit("newUser", user)
       })

Client

  1. Install socket.io-client package

  2. Import it where needed

  3. On constructor, connect to server:

    this.socket = io("URL_OF_THE_BACKEND")
  4. Send message to server:

    this.socket.emit("messageSent", message)
  5. When component loads (componentDidMount), listen for new messages and add them to the state:

    this.socket.on("newMessage", message =>{
    	const messages = [...this.state.messagesList]
    	messages.push(message)
    	this.setState({
    		...this.state,
    		messagesList: messages
    	})
    })

react-socket-chat's People

Contributors

rubenvillarnet avatar sitobusnan 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.