Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between io.on and socket.on in Socket.io?

Tags:

socket.io

I am confused on what the 'socket' parameter is that is passed with the function (In 'The enigma' section). Then the parameter gets used 'socket.on'. What is the difference between io.on and socket.on?

The following code is slightly adapted from the Socket.io chat application example.

Variables

var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app)
var io = require('socket.io').listen(server);

The enigma

io.on('connection', function (socket) {
  console.log('user connected');
  socket.on('message', function(msg) {
    console.log('message: ' + msg);
    io.emit('message', msg);
  })
});

Start server

server.listen(3000, function() {
  console.log('server is running');
});

index.jade

body
  script(src="/socket.io/socket.io.js")

form(method='post', action="/")
  input(type='text', id='user', autocomplete='off')
  input(type='submit', onClick="myFunc()")

strong messages:
  p(id="messages")

script.
  var socket = io();

  socket.on('message', function(msg) {
    console.log('client: ' + msg);
  });

  function myFunc() {
    var text = document.getElementById('user');
    socket.emit('message', text.value);
    text.value = '';
  };
like image 541
Scott Avatar asked Nov 13 '15 23:11

Scott


People also ask

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

What does Io mean in Socket?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.

Which is better Socket.IO or pusher?

Pusher is the category leader in delightful APIs for app developers building communication and collaboration features. On the other hand, Socket.IO is detailed as "Realtime application framework (Node. JS server)". Socket.IO enables real-time bidirectional event-based communication.

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.


1 Answers

In your code example, io is a Socket.IO server instance attached to an instance of http.Server listening for incoming events.

The socket argument of the connection event listener callback function is an object that represents an incoming socket connection from a client.

Both of them can listen for events with the on method.

It might help you visually understand how the two are separate if you re-imagine your code sample like this:

var connectionEvent = function(socket) {
    console.log('user connected');
    socket.on('message', function(msg) {
        console.log('message: ' + msg);
        io.emit('message', msg);
    });
};

io.on('connection', connectionEvent);
like image 57
Connor Avatar answered Oct 11 '22 13:10

Connor