I got a post function to add new user. I dont see any need to return anything to the client. Will the node thread stuck until he find a respond to send back? Is this a good approach?
(I use express)
app.post('/newUser',function(req,res){
users.push(req.body.user);
});
You should always return something to the client. The client waits for a response.
In the case of adding a user, you could return the created user or, if it fails, an error message.
Related
I have a webhook that runs on message save and message update. I do some basic bad word filtering. I can update the message to filter out bad words. However, when I attempt to flag the message, I get an error. Has anyone seen anything like this? How have you worked around it?
The code:
client.flagMessage(message.id).then(r => console.log('flagged message', r))
I have verified that client works as I am able to update the message with client in the same Promise.all() call.
The error:
Flag failed with error: "either user or user_id must be provided when using server side auth."
Version:
"stream-chat": "^1.7.4"
The docs:
https://getstream.io/chat/docs_rest/#flag
https://github.com/GetStream/stream-chat-js/blob/master/src/client.js#L1227
It seems very similar to this closed issue:
https://github.com/GetStream/stream-chat-js/issues/113
This might not be a proper answer but definitely is a resolution for your problem.
If setUser is called on the client then server will be able to get the flagging user from the JWT (client side auth) but in server side auth, there is no user passed to server so you get the expected error message.
You check REST docs and as seen, server supports it where JS client lacks this server side support. This a missing feature bug from JS client. It's reported to be extended as soon as possible.
In a Express application I would like to do something like this:
app.get('/some/route', someMiddleWare(), function(req, res){
var status = undefined;
if( /*someCondition*/ ) status = 200;
else status = 403
res.status(status).send('');
// do something else
})
In the first part I do something that is needed in order to decode what response to give, in the second (after send()) I do something additional that needs to be done in the same execution (i.e. not asynchronously) but does not really concern the user.
The question is: can I be sure that after send() returns the response is already on its way back to the user? Or is it sent only after the execution of my handler function?
Thanks
Yes. You're code will execute in the function even after you send the data through res.send().
However, after you use res.send() you will no longer be able to send any other data with that same response to that request.
The question is: can I be sure that after send() returns the response
is already on its way back to the user? Or is it sent only after the
execution of my handler function?
Yes, you can be sure the data is on its way. At the point you call res.send(), the data has been written to the underlying TCP infrastructure and it is then up to the TCP stack to send it. Due to TCP Nagle algorithm, there might be a short delay before the data is sent, but it will not be waiting for anything in your Express application. The data will be sent as soon as the TCP stack decides to send it.
It won't matter whether you are doing something else in the request handler after you send the response or not.
If you want to follow the actual source code for this, you first look at res.send(data) in express and that ultimately ends up calling res.end(data) in the http module. And, by looking at the http module, you can see that it is writing the data to the TCP implementation synchronously when it is called. There are no magic delays waiting for other things to happen in Express.
We need to update the client side UI to indicate that a message fails to deliver. How do I have Socket.IO JS client call a custom callback directly when the message fails to deliver? For example, something like:
socket.emit("event", data).onError(myCallback);
I know Socket.IO provides the Ack mechanism to confirm delivery success. Therefore, one can set up a timer with a handler which calls the failure callback, if the ack is not called after a certain amount of time. But this doesn't seem to be the best way to do.
Also there is the error event provided by Socket.IO, but it doesn't come with info regarding which emit caused the error.
Unfortunately there's no way to get errors from callbacks, the only way is to indeed create your own timeout:
var timeoutId = setTimeout(timeoutErrorFn, 500);
var acknCallbackFn = function(err, userData){
clearTimeout(timeoutId)
//manage UserData
}
socket.emit('getUserData', acknCallbackFn);
Source of the code
And there's another issue about this, open
So for the time being you have to stick with your manual setTimeout.
Is it possible to send a response from express, and wait for a return response before continuing?
A typical scenario is something like this
Server A sends a request to server B.
Server B processes the request and sends to back to server A
Server B waits for a response from server A before continuing
Server A does further processing of the response from Server B and sends it back to Server B
Server B then handles the rest of the processing required.
My understanding is that normally this is handled with callbacks. In express I would expect to do something like
res.write('response', callback);
function callback() {
//do stuff
}
I don't see that this is possible with the res.write method though. Is there another method I can use with express to get this functionality? I've never used socket.io before, but this seems like a scenario where websockets would be useful. Am I wrong in this assumption?
res.on('finish', callback);
is sent when the last of the data is given to the OS to deal with.
http://nodejs.org/api/http.html#http_event_finish
If you need to know when the client receives/processes the data, however, the client must send something back to the server, in which case socket.io could help.
I appreciate all the responses and help from everyone, I ended up using sessions to get what I needed.
var session = require('express-session');
Thanks again
I'm using socket stream to send the data to the logged in user by using the following code
var ss = require('socketstream');
....
....
ss.api.publish.user('userId', content);
but the ss.api.publish is undefined is what the error i'm receiving.
Where am i going wrong.
Please advice.
The API for a publish to a user is:
Sending to Users
Once a user has been authenticated (which basically means their session now includes a value for req.session.userId), you can message the user directly by passing the userId (or an array of IDs) to the first argument of ss.publish.user as so:
// in a /server/rpc file
ss.publish.user('fred', 'specialOffer', 'Here is a special offer just for you!');
Important: When a user signs out of your app, you should call req.session.setUserId(null, cb) to prevent the browser from receiving future events addressed to that userId. Note: This command only affects the current session. If the user is logged in via other devices/sessions these will be unaffected.
The above is taken from the original document describing the socketstream pub/sub api.
as you can see, you need to supply one more argument than you thought. That is, becasue on the client side you need to subscribe to a message channel in order to get the message. In the above example, you need to do this in your client side code:
ss.event.on('specialOffer', function(message){
alert(message);
});