2

So I'm trying to move my project from development to production, hosted with modulus.io.

The problem is that every time I try to emit or receive data between the server <-> client connection, the application fails on the server's end.

This is the error I get:

/mnt/data/2/node_modules/socket.io/lib/parser.js:75
      data = JSON.stringify(ev);
                  ^
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at Object.exports.encodePacket (/mnt/data/2/node_modules/socket.io/lib/parser.js:75:19)
    at Socket.packet (/mnt/data/2/node_modules/socket.io/lib/socket.js:212:21)
    at Socket.emit (/mnt/data/2/node_modules/socket.io/lib/socket.js:368:15)
    at null.<anonymous> (/mnt/data/2/server/*******/*******.coffee:36:52)
    at wrapper [as _onTimeout] (timers.js:252:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Here's where I am with debugging:

  • This is the line where the error is, though it is sometimes elsewhere, depending on who tries to emit an event first:

    Vent.socket?.emit('event', @)

  • Socket.io works as expected on localhost

  • Socket.io connects successfully to the client before failing (from chrome dev tools):

    • XHR finished loading: "http://********.onmodulus.net/socket.io/1/?t=1372727710817". socket.io.js:1659

    • XHR finished loading: "http://********.onmodulus.net/socket.io/1/xhr-polling/sKNz7RX5j2fo-Rku8bDb?t=1372727711281".

  • Socket.io will connect on the server and run until I try to either emit or respond to an event

Also if it helps here's my Event Aggregator class:

module.exports = class Events extends events.EventEmitter

  constructor: (io) ->
    self = @
    @io = io
    @socket = null

    @io.configure(
        () =>
            console.log 'configuring socket.io...'.yellow
            @io.set("transports", ["xhr-polling"])
            @io.set("polling duration", 10)
    )

    @io.sockets.on('connection', 
      (socket) =>
        @socket = socket
        console.log 'socket connected'

        socket.on('client', (event, arg) ->
            if arg?
                self.emit(event, arg)
            else 
                self.emit(event)
        )
    )
Connor Black
  • 6,921
  • 12
  • 39
  • 70

1 Answers1

1

After much hair-pulling debugging I discovered that when JSON.stringify() tries to stringify a circular object, i.e. a self referencing object, the function will infinitely run due to recursion.

Chrome sendrequest error: TypeError: Converting circular structure to JSON

So when I tried to run the below code, @ (equivalent to this in coffeescript) was self referencing, and therefore created an infinite loop and crashed socket.io.

Vent.socket?.emit('event', @)
Community
  • 1
  • 1
Connor Black
  • 6,921
  • 12
  • 39
  • 70