question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

geting multiple messages

See original GitHub issue

Hi,

I am using socket.io for chating. mycode: app.js:

var express = require(‘express’); var app = express(); var http = require( “http” ).createServer( app ); var io = require( “socket.io” )( http ); var port = process.env.PORT || ‘8088’; http.listen(port); app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘jade’); app.use(‘/’, routes); app.use(‘/users’, users); app.use(function(req,res,next){ req.io = io; next(); }); module.exports = app;

index.js:

var express = require(‘express’); var router = express.Router(); router.get(‘/’, function(req, res, next) {
res.render(‘index’, { title: ‘Title’ });
}); router.get(‘/chat’, function(req, res) {
// usernames which are currently connected to the chat var usernames = {}; var numUsers = 0; var io=req.io; io.on(‘connection’, function (socket) {console.log(“5”); var addedUser = false;

  // when the client emits 'new message', this listens and executes
  socket.on('new message', function (data) {
    // we tell the client to execute 'new message'
    socket.broadcast.emit('new message', {
      username: socket.username,
      message: data
    });
  });

  // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {

    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    //usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected

    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

  // when the client emits 'typing', we broadcast it to others
  socket.on('typing', function () {
    socket.broadcast.emit('typing', {
      username: socket.username
    });
  });

  // when the client emits 'stop typing', we broadcast it to others
  socket.on('stop typing', function () {
    socket.broadcast.emit('stop typing', {
      username: socket.username
    });
  });

  // when the user disconnects.. perform this
  socket.on('disconnect', function () {
    // remove the username from global usernames list
    if (addedUser) {
      delete usernames[socket.username];
      --numUsers;
      // echo globally that this client has left
      socket.broadcast.emit('user left', {
        username: socket.username,
        numUsers: numUsers
      });
    }
  });
});
res.render('chat', { title: 'Chat' });

}); module.exports = router;

chat.jade:

doctype html html(lang=“en”) head meta(charset=“UTF-8”)
link(rel=“stylesheet”, href=“/stylesheets/style.css”) body ul.pages li.chat.page .chatArea ul.messages input.inputMessage(type=“text”, placeholder=“Type here…”) li.login.page .form h3.title What"s your nickname? input.usernameInput(type=“text”, maxlength=“14”) script(src=“https://code.jquery.com/jquery-1.10.2.min.js”) script(src=“/javascripts/socket.io.js”) script(src=“/javascripts/main.js”)

When run the above script working fine, but getting multiple messages Ex: if i refresh the page n times getting n messages

Help me.

Thanks Pokal

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

45reactions
PritamUpadhyaycommented, Jul 9, 2015

use socket.removeAllListeners() at start up.

0reactions
BodyaNKcommented, May 29, 2021

use socket.removeAllListeners() at start up.

Thanks man!

Read more comments on GitHub >

github_iconTop Results From Across the Web

I Am Receiving Multiple Duplicate Text Messages
So, if your issue with the whole duplicate text is billing, check your text message details. Visit www.vzw.com > Sign in as the...
Read more >
How to Stop Sending Duplicate Text Messages on Android
A variety of problems can cause duplicate messages on Android and this article will teach you how to fix that from happening.
Read more >
6 Ways to Solve Receiving Duplicate Text Messages Samsung
Method 1: Force Restart Samsung · Hold the power button and the volume button of your phone down for roughly 7 seconds. force...
Read more >
Why Do I Receive Duplicate Text Messages On Android? 7 ...
Tutorial on Fixing Receiving Duplicate Text Messages on Android · Fix #1 Uninstall the 2nd Messaging App · Fix #2 Restart Your Phone...
Read more >
Receiving the Same Text Message Repeatedly on Samsung ...
First, make sure your software is up to date by going into Settings > About Device > Software Update.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found