help | Adding an array to the user's db
-
I'm just now trying to develop some kind of system through a socket on the server, if you could help me regardless. Thank you very much!
I don't know how to perform a callback in a socket, for example:
I loaded a socket on the client side, and on the server side a callback of a command on the client side is defined - like app.alert, so when I load the socket on the client side, it will execute app.alert for me - as defined in the extension of the socket on the server side.
This is exactly what zany is looking for, only I don't exactly know how to set it up, I would really appreciate it if you could help me with this! -
Sounds like you are trying to send an event from server to client. You can do that with
socket.emit('someEvent', data);
on the server side. And on the client side you listen to that event withsocket.on('someEvent', function (data) { });
.There are plently of examples of this in nodebb if you search for socket.emit/on. NodeBB uses socket.io so their documentation is also useful. https://socket.io/docs/v4/emitting-events/
-
In that case you can simply use
socket.emit
on the client side and respond to that with data from the server side. You can see how it's done in a plugin like https://github.com/NodeBB/nodebb-plugin-category-notifications. Client sends here and server responds here -
Thanks!
Apparently I was looking for this command:throw new Error('נתונים שגויים');
But is this the only way to send events to the client?
If I write console.log() on the server side, does it print to the console on the client side?throw console.log()
-
throw new Error('testing')
is used for alerting the client side about an error. The reason you are seeing that on the client side is because the server side is catching the error and sending the error message as a string to the client. That happens here.If you want to send custom messages to the client you can just return them from your socket function on the server. Here is simple example.
// on the client socket.emit('plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } console.log('server returned', returnData); }); // on the server const sockets = require.main.require('./src/socket.io/plugins'); sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
-
@baris Don't know why, but it doesn't work...
Server side:
SocketPlugins.myPlugin = function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
I ran client side:
socket.emit('admin.plugins.testin', { data: 1 }, function (err, returnData) { if (err) { return app.alertError(err); // if server throws an error it will be displayed } console.log('server returned', returnData); });
And this is the response:
undefined
-
@LEVI-HAVIV the name you use in the socket.emit needs to match the name of the function on the server side. For a socket function in the admin namespace the below sample should work
// on the client socket.emit('admin.plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } console.log('server returned', returnData); }); // on the server const sockets = require.main.require('./src/socket.io/admin/plugins'); sockets.myPlugin = {}; sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
Keep in mind when a socket event starts with
admin.
only users who are administrators will be able to call this. -
Oh! Thank you very much!
I didn't realize that was the problem...
In any case, is it possible to transmit js commands to the client? And not text or arrays?
js example:return console.log('test');
Thank you very much for the help so far
-
I don't think you can send javascript functions, you would have to send a string and then run a command based on that string. A sample below
// on the client socket.emit('admin.plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } if (returnData.log) { console.log(returnData.log); } }); // on the server const sockets = require.main.require('./src/socket.io/admin/plugins'); sockets.myPlugin = {}; sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { log: 'this message will be logged client side' }; };