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.

Emit function parameters (Client and Server) do not mirror Socket.io API

See original GitHub issue

Hi @thoov,

I have been using your library at work for unit testing a Socket.io based application we are making. I have contributed to the library in Issue #79 in order to add some missing functionality we needed in our app.

When running tests I am running into an issue were the API for mock-socket’s emit function is not aligned with Socket.io’s emit function.

The Socket.io API for emit is emit(event, ...data), which allows for variable amounts of data args to be passed to the callback function listening to the specified event name.

The mock-socket API for emit is emit(event, data, options) for the server and emit(event, data) for the client. The mock-socket API does not allow for variable amounts of data args to be passed. We could pass all of our data as an object, but I feel this incorrect.

I looked into making the changes necessary to correct this but it would be a major change to libraries API. I would be willing to do the work to make the modifications to align the API between the two libraries. Before doing this work I wanted first to talk to you about why the API was structured this way and your ideas on how to correct it.

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rocketedawaycommented, Aug 2, 2016

@thoov Congrats on getting married!! It’s a crazy and fun day 😃

Okay based on your example you are only printing the first argument. To access all the data being returned by the emit callback in your example you would have to do the following:

socket.on('clientEmit', function (dataFoo, dataBar, dataBaz) {
    console.log(dataFoo, dataBar, dataBaz); // OR console.log(arguments)
});

Following are code examples to see this in action:

client.js

const io = require('socket.io-client');

const client = io.connect('http://localhost:8080');
client.on('connect', function () {
    console.log('Conected to IO server: localhost:8080');
    client.emit('clientEmit', 'foo', 'bar', 'baz');
});

server.js

const express = require('express');

const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

server.listen(8080, function () {
    console.log('Server listening on localhost:8080');
});

io.on('connect', function (socket)  {
    socket.on('clientEmit', function () {
        console.log('Socket Event: clientEmit', arguments);
    });
});

The above console logs the following from the server

Server listening on localhost:8080
Socket Event: clientEmit { '0': 'foo', '1': 'bar', '2': 'baz' }

Thanks! Let me know if you need anything else from me!

2reactions
thoovcommented, Jul 29, 2016

@rocketedaway Sorry for the delay. I just got married so its been crazy the last couple of weeks with that.

I ran the example code that you provided and was able to see the same results as you pointed out. However, I created this example:

index.html

<html>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost:8080');

  socket.on('connect', function () {
    socket.emit('clientEmit', 'foo', 'bar', 'baz');
  });

  socket.on('serverEmit', function (data) {
    console.log(data);
  });
</script>
</html>

server.js

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('serverEmit', 'foo', 'bar', 'baz');

  socket.on('clientEmit', function (data) {
    console.log(data);
  });
});

On both the server and client I just see ‘foo’ which it appears socket.io is just taking the first argument. This is what mock-socket is currently doing. Do you have an example using pure socket.io where multiple arguments are passed? (I tested against the latest 1.4.5)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emitting events | Socket.IO
IO API is inspired from the Node.js EventEmitter, which means you can emit events on one side and register listeners on the other:....
Read more >
Client API - Socket.IO
Emits an event to the socket identified by the string name. Any other parameters can be included. All serializable datastructures are supported, ...
Read more >
Client API - Socket.IO
Emits an event to the socket identified by the string name. Any other parameters can be included. All serializable datastructures are supported, ...
Read more >
Server API - Socket.IO
Closes the Socket.IO server and disconnect all clients. The callback argument is optional and will be called when all connections are closed.
Read more >
Server API | Socket.IO
If no arguments are supplied this method returns the current value. ... client from the incoming engine.io (or compatible API) Socket .
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