Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:08:26+00:00 2026-06-01T02:08:26+00:00

So, I have been trying to understand Socket.io lately, but I am not a

  • 0

So, I have been trying to understand Socket.io lately, but I am not a supergreat programmer, and almost every example I can find on the web (believe me I have looked for hours and hours), has extra stuff that complicates things. A lot of the examples do a bunch of things that confuse me, or connect to some weird database, or use coffeescript or tons of JS libraries that clutter things up.

I’d love to see a basic, functioning example where the server just sends a message to the client every 10 seconds, saying what time it is, and the client writes that data to the page or throws up an alert, something very simple. Then I can figure things out from there, add stuff I need like db connections, etc. And yes I have checked the examples on the socket.io site and they don’t work for me, and I don’t understand what they do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:08:27+00:00Added an answer on June 1, 2026 at 2:08 am

    Edit: I feel it’s better for anyone to consult the excellent chat example on the Socket.IO getting started page. The API has been quite simplified since I provided this answer. That being said, here is the original answer updated small-small for the newer API.

    index.html

    <!doctype html>
    <html>
        <head>
            <script src='/socket.io/socket.io.js'></script>
            <script>
                var socket = io();
    
                socket.on('welcome', function(data) {
                    addMessage(data.message);
    
                    // Respond with a message including this clients' id sent from the server
                    socket.emit('i am client', {data: 'foo!', id: data.id});
                });
                socket.on('time', function(data) {
                    addMessage(data.time);
                });
                socket.on('error', console.error.bind(console));
                socket.on('message', console.log.bind(console));
    
                function addMessage(message) {
                    var text = document.createTextNode(message),
                        el = document.createElement('li'),
                        messages = document.getElementById('messages');
    
                    el.appendChild(text);
                    messages.appendChild(el);
                }
            </script>
        </head>
        <body>
            <ul id='messages'></ul>
        </body>
    </html>
    

    app.js

    var http = require('http'),
        fs = require('fs'),
        // NEVER use a Sync function except at start-up!
        index = fs.readFileSync(__dirname + '/index.html');
    
    // Send index.html to all requests
    var app = http.createServer(function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(index);
    });
    
    // Socket.io server listens to our app
    var io = require('socket.io').listen(app);
    
    // Send current time to all connected clients
    function sendTime() {
        io.emit('time', { time: new Date().toJSON() });
    }
    
    // Send current time every 10 secs
    setInterval(sendTime, 10000);
    
    // Emit welcome message on connection
    io.on('connection', function(socket) {
        // Use socket to communicate with this particular client only, sending it it's own id
        socket.emit('welcome', { message: 'Welcome!', id: socket.id });
    
        socket.on('i am client', console.log);
    });
    
    app.listen(3000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to understand these but every article, wiki etc. says something
I have been trying to understand this algorithm for past two hours, but can't
I have been trying to understand the way ActionScript's events are implemented, but I'm
For last 48 hours, I have been trying to understand Multithreading and Socket Programming
I have been trying to understand why it will not work for me. Im
It maybe a little naive question but I have been trying to understand how
I have been trying to understand how to return links from yql results, but
I have been trying to understand function pointers in C++ so that I can
I have been trying to understand the difference between different references but the theory
I have been trying to understand the concepts of Maven and I find it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.