0

i'm implementing a web socket in nodejs to make a query to dynamodb but appear this error: buffer.js:398 Buffer.isBuffer = function isBuffer(b) { ^

RangeError: Maximum call stack size exceeded
    at Function.isBuffer (buffer.js:398:36)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:44:66)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)

.....here is my code:

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


            const AWS = require('aws-sdk');
            AWS.config.update({ region: 'us-west-2' });
            docClient = new AWS.DynamoDB.DocumentClient();
            const tableName = 'Dispositivo';

            var datos=docClient.scan({
                TableName: tableName
            },(err, data)=>{
                if(err) {
                    console.log(err);
                } else {
                    console.log(data);
                }
            });

            io.on('connection', (socket) => {

                // Log whenever a user connects
                console.log('user connected');

                // Log whenever a client disconnects from our websocket server
                socket.on('disconnect', function(){
                    console.log('user disconnected');
                });

                // When we receive a 'message' event from our client, print out
                // the contents of that message and then echo it back to our client
                // using `io.emit()`
                socket.on('message', (message) => {
                    console.log("Message Received: " + message);
                    io.emit('message', {type:'new-message', text: datos});    
                });
            });

            // Initialize our websocket server on port 5000
            http.listen(5000, () => {
                console.log('started on port 5000');
            });
dmcgrandle
  • 5,934
  • 1
  • 19
  • 38
Steven Giraldo
  • 329
  • 1
  • 3
  • 12

1 Answers1

0

You are trying to emit a function that is stored inside the variable datos. Since that function is asynchronous you are not actually storing the data that you get inside the callback function. You are console.logging it but you are not storing it on the variable. That's the reason io.emit fails. I recommend you read on how asynchronous functions work in javascript. Read more about this problem that many new javascript developers have here: How do I return the response from an asynchronous call?

itsundefined
  • 1,409
  • 2
  • 12
  • 32