-3

I am a newbie in Nodejs and I actually don't still understand the mission of two method : "on" and "emit" in socket.io

Sorry .Let me guess:

  • "on" method is listen from client/server

  • "emit" to broadcast a event to client/server

And questions are :

1.Can we create a custom event beside built-in events ?

What does socket.io apply for ?

  • 1
    What does socket.io apply for ? Is this a Test? Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Stamos Jan 22 '19 at 08:03

1 Answers1

-1

Yes, you can create custom events and "emit" then and detect them using "on", Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a Node.js, may be i am not clearly understanding second part of your question about "What Socket.IO apply for" but i guess if you will google ,you might get some more insight into library.

`//in node js i.e. server side
socket.on('has connected', function (username) {
        console.log('connected Udsfh');
        username = username;
        users.push(username);
        console.log(username);
        io.emit('has connected', users);
    });`


`//client side--just a sample to catch the emitted event and use it
var socket = io();
 socket.on('has connected', function (users) {
        for (var i = 0; i < users.length; i++) {
            $("users").append("<li><b>" + users[i] + "</b></li>");
        }
    });