Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chriswitko/5f8dba204ce6a9f6c0f06118f95799a3 to your computer and use it in GitHub Desktop.

Select an option

Save chriswitko/5f8dba204ce6a9f6c0f06118f95799a3 to your computer and use it in GitHub Desktop.

Revisions

  1. @luciopaiva luciopaiva revised this gist Feb 21, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions _Full-socketio-client-and-server-example.md
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@ Create a folder, run `npm init -f` on it and paste both `server.js` and `client.

    Install the required libraries:

    npm install socket.io
    npm install socket.io-client
    npm install socket.io@3.1.1
    npm install socket.io-client@3.1.1

    Run the server:

  2. @luciopaiva luciopaiva revised this gist Feb 21, 2021. 2 changed files with 4 additions and 2 deletions.
    2 changes: 2 additions & 0 deletions _Full-socketio-client-and-server-example.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Full socket.io client and server example

    *Last updated: 2021-02-21, tested with socket.io v3.1.1*

    This is the simplest implementation you will find for a client/server WebSockets architecture using socket.io.

    To see a full explanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.
    4 changes: 2 additions & 2 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@

    const
    io = require("socket.io"),
    server = io.listen(8000);
    {Server} = require("socket.io"),
    server = new Server(8000);

    let
    sequenceNumberByClient = new Map();
  3. @luciopaiva luciopaiva revised this gist Nov 19, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _Full-socketio-client-and-server-example.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Full socket.io client and server example

    This is the most keep-it-simple implementation you will find for a client-server web sockets architecture using socket.io.
    This is the simplest implementation you will find for a client/server WebSockets architecture using socket.io.

    To see a full explanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.

  4. @luciopaiva luciopaiva revised this gist Nov 19, 2019. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion _Full-socketio-client-and-server-example.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,15 @@
    # Full socket.io client and server example

    This is the most keep-it-simple implementation you will find for a client-server web sockets architecture using socket.io.

    To see a full explanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.

    ## How to use
    If you're looking for examples using frameworks, check these links:

    - [socket.io + Express](https://github.com/luciopaiva/socketio-with-express)
    - [socket.io + Restify](https://github.com/luciopaiva/socketio-with-restify)

    ## How to run

    Create a folder, run `npm init -f` on it and paste both `server.js` and `client.js` there (see files below). Needless to say, you must have Node.js installed on your system.

  5. @luciopaiva luciopaiva renamed this gist Oct 16, 2019. 1 changed file with 0 additions and 0 deletions.
  6. @luciopaiva luciopaiva renamed this gist Oct 16, 2019. 1 changed file with 0 additions and 0 deletions.
  7. @luciopaiva luciopaiva revised this gist Oct 1, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    To see a full exaplanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.
    # Full socket.io client and server example

    # How to use
    To see a full explanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.

    ## How to use

    Create a folder, run `npm init -f` on it and paste both `server.js` and `client.js` there (see files below). Needless to say, you must have Node.js installed on your system.

  8. @luciopaiva luciopaiva created this gist Oct 1, 2017.
    18 changes: 18 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    To see a full exaplanation, read my answer on SO here: https://stackoverflow.com/a/24232050/778272.

    # How to use

    Create a folder, run `npm init -f` on it and paste both `server.js` and `client.js` there (see files below). Needless to say, you must have Node.js installed on your system.

    Install the required libraries:

    npm install socket.io
    npm install socket.io-client

    Run the server:

    node server

    Open other terminal windows and spawn as many clients as you want by running:

    node client
    6 changes: 6 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@

    const
    io = require("socket.io-client"),
    ioClient = io.connect("http://localhost:8000");

    ioClient.on("seq-num", (msg) => console.info(msg));
    28 changes: 28 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    const
    io = require("socket.io"),
    server = io.listen(8000);

    let
    sequenceNumberByClient = new Map();

    // event fired every time a new client connects:
    server.on("connection", (socket) => {
    console.info(`Client connected [id=${socket.id}]`);
    // initialize this client's sequence number
    sequenceNumberByClient.set(socket, 1);

    // when socket disconnects, remove it from the list:
    socket.on("disconnect", () => {
    sequenceNumberByClient.delete(socket);
    console.info(`Client gone [id=${socket.id}]`);
    });
    });

    // sends each client its current sequence number
    setInterval(() => {
    for (const [client, sequenceNumber] of sequenceNumberByClient.entries()) {
    client.emit("seq-num", sequenceNumber);
    sequenceNumberByClient.set(client, sequenceNumber + 1);
    }
    }, 1000);