-1

When the client connects to the server a message is supposed to be emitted to the console. I'm not getting any errors so I'm confused as to what my problem actually is.

Server: As you can see the client connects. enter image description here
Client: The message doesn't appear in the console. client image
(Forgive me for the links, I don't have 10 reputation)

How do I get the message to print to the console?

I've read other posts like this one, but they weren't helpful :(

linktoahref
  • 7,812
  • 3
  • 29
  • 51
Darkauro
  • 3
  • 1
  • 3
  • Questions here about code MUST include the relevant code pasted into the question and formatted appropriately to be easy to read. External links are allowed, but NOT as the only reference to your code because external links have a habit of changing or disappearing over time rendering the question useless as a long term search reference (which an important part of stack overflow's mission). And, code should never be included as an image, always as properly formatted text. – jfriend00 Jul 25 '17 at 02:07
  • Code should be pasted into your question as TEXT, not as images. Images can't be copied from and can't be searched. – jfriend00 Jul 26 '17 at 02:03

2 Answers2

1

When you do io.connect(), that call is asynchronous and not immediate. You cannot immediately emit to the server until the client generates the connect event:

 var socket = io.connect()
 socket.on('connect', function() {
     // it is safe to call `.emit()` here
     socket.emit("sndMsg", someData);
 });
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @Darkauro - If you include more of your code in your question as text (not as images), then we might have an idea how to help further. Have you checked the console in both client and server for errors? Direct from the stack overflow help center, you should be including a [Minimal, Complete and Verifiable](https://stackoverflow.com/help/mcve) example of your problem. Among other things, that means everything we might need to reproduce the problem. – jfriend00 Jul 26 '17 at 02:06
  • Thank you. This helped a ton. Waiting for the client to connect before emitting was one issue I had. After fixing it, I also realized that I set the io.connect to the wrong url. Sorry for the low quality post, I'm really new and Ill make sure to follow the guidelines next time :) – Darkauro Jul 26 '17 at 02:13
0

index.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>

server.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);
Jijo Paulose
  • 1,896
  • 18
  • 20