GithubHelp home page GithubHelp logo

tlanedev / react-and-socketio Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 183 KB

A simple demo of how to implement socket.io into a react application.

JavaScript 92.34% HTML 7.66%
react socket-io-client socket-io-server express

react-and-socketio's Introduction

React, Express and Socket.io

In another project, I wanted to build a react application on an express server that utilized socket.io to allow users to interact in realtime. Here, I wanted to strip away all of the unnecessary parts and build a working prototype with those features.

Note, in this app, I have setup two different backends to explore some possbilities (one is server.js and the other is pairingserver.js). One can switch between the two setups by using the two different npm scripts "npm run start" or "npm run start-pair", respectively.

The Most Important Parts

  • You must install a socket on the frontend and the backend of the web app. (The socket on the backend is sometimes called "socker" as an abbreviation of "socket server")

  • The (backend) socker is brought online by:

    const socket = require('socket.io');
    const io = socket(server);
  • You must write your own "protocol" for the (frontend) client sockets and the (backend) socker to communicate.

  • The backend "protocol" is set up with something like:

    io.on('connection', (socket) =>{
        console.log('browser communicated with server');
        socket.on('button', data => {
            console.log(data);
            io.sockets.emit('button');
        });
        socket.on('client-event', () => {
            //do something on the backend
            const data = "data to be returned to frontend";
            io.sockets.emit('client-event-has-been-handled-by-backend', data)
        })
    })
  • The client socket can be stored in a different file separate from the react components and imported into relevant components. We use a function like the following to handle the client-side protocol:

    export function socketIsListening(){
        socket.on('button', () => console.log('server emitted to client socket'));
        socket.on('backend-event', () => {/*do something else*/})
        socket.on('client-event-has-been-handled-by-backend', () => {
            this.setState({
                //updates state
            });
        });
    }
  • Since we want the client socket to be able to update the state of our react components directly, we include the following in the component class constructor.

        class Component extends React.Component{
            super(props);
            this.state = {
                // ...
            }
            socketIsListening.bind(this)();
            /*
                ...
                //more react stuff
                ...
            */
        }

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.