async/await migration issue: Data doesn't reach the plugin after a socket emit

Plugin Development
  • Re: Problem with socket in plugin after 1.12.2 -> 1.13.0 upgrade

    Hello,

    In a related scenario to the referenced topic, I have a problem migrating my plugins to conform with async/await calls. Looks like the returned data doesn't get to the calling client-side function.
    Here is my test code in async/await form. Please note these functions work in old callback form:

    var SocketPlugins = require.main.require('./src/socket.io/plugins');
    SocketPlugins.employees.getDetail = async function (socket, data) {
    	if (!data || !data.empId) {
    		return null;
    	}
    	console.log('Got these parameters:', data); // params gets here as it should
    	... do something  ...
    	var person = { name: 'jimmy johnes', id: '007', address: '123 happy street', phone: '800-324-2344' };
    	return person; **// Data is not going back to client** 
    }
    
    async function getEmployeeDetail(empId) {	
    	var employees = await socket.emit('plugins.employees.getDetail', {
    		empId: empId,
    	});
    	console.log('Received callback from employees.getDetail:', employees); 
    *// returned data is not correct and appears to be the callback object ?!?!!?*
    }
    

    Client-side console:

    Received callback from employees.getDetail: Tue Jan 28 2020 09:24:29 GMT-0500 (Eastern Standard Time)
    i {io: i, nsp: "/", json: i, ids: 11, acks: {…}, …}*

    Thank you for your guidance.

  • Are you calling socket.emit from the server-side (async function getEmployeeDetail)? It should be called from the client-side

  • @julian sorry if I confused you.

    getEmployeeDetail is Client-side function that emits the socket call to invoke server-side function SocketPlugins.employees.getDetail.

    I verified that the call flow is being performed correctly and data from client is sent to server as those logs indicates.

    The only issue is the client-side expects to get the 'person' data from server but somehow employees is populated with some weird callback object instead of the person. Is this correct assumption that returned value is assigned to employees in the following statement:
    var employees = await socket.emit('plugins.employees.getDetail', {empId: empId,});

    Thanks.

    //Server-side 
    var SocketPlugins = require.main.require('./src/socket.io/plugins');
    SocketPlugins.employees.getDetail = async function (socket, data) {
    	if (!data || !data.empId) {
    		return null;
    	}
    	console.log('Got these parameters:', data); // params gets here as it should
    	... do something  ...
    	var person = { name: 'jimmy johnes', id: '007', address: '123 happy street', phone: '800-324-2344' };
    	return person; **// Data is not going back to client** 
    }
    
    // client-side
    async function getEmployeeDetail(empId) {	
    	var employees = await socket.emit('plugins.employees.getDetail', {
    		empId: empId,
    	});
    	console.log('Received callback from employees.getDetail:', employees); 
    *// returned data is not correct and appears to be the callback object instead?!?!!?*
    }
  • I don't think you can do that await on client side. Socket.io just doesn't return a promise, I think.
    While the server-side code should be fine, I think you still need to use a callback if you want an acknowledgement on emit.

  • @oplik0 I believe you are right. I am going wait longer and hope the support to be added later in the future. Thank you,

  • You can always add support for promises client side yourself. Give this a try.

    var myEmit = async function emit(event, data) {
        return new Promise(function (resolve, reject) {
             socket.emit(event, data, function (err, result) {
                  if (err) reject(err);
                  else resolve(result);
             });
        });
    }
    

    And then

    var data = await myEmit('posts.getPost', 1);
    console.log(data);
    
  • @baris Your solution was confirmed working for my plugin. Thank you Sir.


Suggested Topics