Chat server + AJAX (client side)

#1

Hello,
I would like to achieve the following thing. I want to have a server in Node-red which can be used for chat - but only server! I want to make AJAX (really AJAX, not sockets) on client side in my IDE (not in node-red).

How can I do that? How can I build the server in order to be able to use AJAX on clients side in my IDE?

Thank you

0 Likes

#2

Node-RED is built using Node.js and ExpressJS. So you already have a server available to you if you are writing custom nodes. Actually, NR uses 2 servers, one for admin and one for user.

You can use those in your own custom nodes.

If you don't want a custom node, you can use the http-in node which adds a listening path to the existing user server. Check out the docs for that.

0 Likes

#3

Thanks for your reply
How would look the flow? Is it possible like this?

Or how should it look like?

0 Likes

#4

Yes, that is the basic starting point.

0 Likes

#5

Ok, thanks. How would it work now? Users will join on the page: http://127.0.0.1:1880/chat, right? Then what should happen in that function? or will it stay the same? (return msg)

0 Likes

#6

@Arth I suggest you start by looking at the cookbook which has a number of examples of using the HTTP nodes - https://cookbook.nodered.org/http/

With the flow you have, it will be triggered when the browser goes to /chat. The browser will receive whatever msg.payload you pass on to the HTTP Response node.

So in this case, that would be the HTML content of the webpage you want them to see. You can use the Template node as a convenient way to provide that content in your flow, rather than the Function node.

0 Likes

#7

thanks,but I do not still understand, how can I perform AJAX in my IDE?

I have the same flow as I posted, but I used template with html code.

0 Likes

#8

Do you know what reply you want from node-red when you call it with your ajax request?

0 Likes

#9

Well, I guess, I would need a message written by someone to display it to other (online) users.

0 Likes

#10

So start with a fixed message, you need to decide if you are going to use true AJAX and send it as xml, or choose to use JSON instead and then use your template node to create the reply message.

You have the URL of your that you defined in your HTTP-IN node.

How you program the client in your IDE isn't really a Node-RED issue...

0 Likes

#11

That is someone beyond the bounds of a Node-RED question perhaps since it is a general web/JavaScript question. You will need to look that up online.

There are various ways you can code AJAX calls from your front-end code. Both using native JavaScript or using a helper library such as jQuery.

You could, of course, make life even easier for yourself and simply use node-red-contrib-uibuilder which sets everything up for you :wink:

0 Likes

#12

Yes, i understand AJAX, I just have a proble to do it with node-red.
I have localhost:1880/chat as an endpoint, where I am sending "message" which was written by user, but in response I am getting EMPTY body. I would need to send back that message in response. What might be wrong?
In my flow I have: Post - function(return msg) - http response

AJAX in my, ide:

0 Likes

#13

Rather than showing us what you have in the client, show us what you have in node-red. Put a debug node on the output of the http node and make sure it is receiving the message ok. If that looks ok then export your flow and paste it here.

[Edit] I should have said then put a debug on the output of the function node and check that shows what you expect, if you still can't work it out then post the flow.

1 Like