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 6720205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:05+00:00 2026-05-26T09:11:05+00:00

I’m trying to make two clients (players) contact each other (exchanging for example strings)

  • 0

I’m trying to make two clients (players) contact each other (exchanging for example strings) through socket.io. I have this code on the clients (gameId is defined back in the code):

var chat = io.connect('http://localhost/play');
chat.emit(gameId+"", {
    guess: "ciao"
});
chat.on(gameId+"", function (data) {
    alert(data.guess);
});

While on the server I have this (which is one of the first things I do, not in routing of course)

var messageExchange = io
    .of('/play')
    .on('connection', function (socket) {
        socket.emit('message', {
            test: 'mex'
        });
      });

Basically I create the channel, then when users connect they use the channel to exchange a message of the king “gameId” that only the both of them can read (using the on.(gameId+"" ... stuff.
My problem is that when players connect (first one, then the other), the first one that connected should alert the data received (because the second one that connected emitted a message). Does anyone of you know why this is not happening?

Thanks.

  • 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-05-26T09:11:06+00:00Added an answer on May 26, 2026 at 9:11 am

    The socket.io server should act like a middle man. It can receive messages from clients and send messages to clients. It doesn’t act as a “channel” by default, unless you have the server relay messages from clients to other clients.

    There’s a lot of good info on common uses on their website, http://socket.io and their repo, https://github.com/LearnBoost/socket.io

    A simple example of a chat client could be something like this:

    var chat = io.connect("/play");
    var channel = "ciao";
    
    // When we connect to the server, join channel "ciao"
    chat.on("connect", function () {
        chat.emit("joinChannel", {
            channel: channel
        });
    });
    
    // When we receive a message from the server, alert it
    // But only if they're in our channel
    chat.on("message", function (data) {
        if (data.channel == channel) {
            alert(data.message);
        }
    });
    
    // Send a message!
    chat.emit("message", { message: "hola" });
    

    While the server could act like this:

    var messageExchange = io
        .of('/play')
        .on('connection', function (socket) {
            // Set the initial channel for the socket
            // Just like you set the property of any
            // other object in javascript
            socket.channel = "";
    
            // When the client joins a channel, save it to the socket
            socket.on("joinChannel", function (data) {
                socket.channel = data.channel;
            });
    
            // When the client sends a message...
            socket.on("message", function (data) {
                // ...emit a "message" event to every other socket
                socket.broadcast.emit("message", {
                    channel: socket.channel,
                    message: data.message
                });
            });
         });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.