# Chat server + AJAX (client side)

**URL:** <https://discourse.nodered.org/t/chat-server-ajax-client-side/9543>\
**Category:** General\
**Created:** [28 March 2019 12:14 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543 "2019-03-28T12:14:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 12:14 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/1 "2019-03-28T12:14:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 March 2019 13:29 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/2 "2019-03-28T13:29:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 13:39 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/3 "2019-03-28T13:39:39Z")

</div>

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

 ![xy](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e283a96ea051e174e9842d425f78c01ee1453473.png)

Or how should it look like?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 March 2019 13:46 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/4 "2019-03-28T13:46:09Z")

</div>

Yes, that is the basic starting point.

---

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 13:54 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/5 "2019-03-28T13:54:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 March 2019 14:07 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/6 "2019-03-28T14:07:54Z")

</div>

@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/](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.

---

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 15:04 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/7 "2019-03-28T15:04:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2019 15:28 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/8 "2019-03-28T15:28:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 15:38 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/9 "2019-03-28T15:38:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [28 March 2019 15:49 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/10 "2019-03-28T15:49:03Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 March 2019 16:08 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/11 "2019-03-28T16:08:31Z")

</div>

> [@Arth](#):
>
> thanks,but I do not still understand, how can I perform AJAX in my IDE?

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 😉

---

<div class="post-metadata">

**Author:** ![Arth](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arth](https://discourse.nodered.org/u/Arth)\
**Post date:** [28 March 2019 18:29 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/12 "2019-03-28T18:29:20Z")

</div>

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:

 ![V%C3%BDst%C5%99i%C5%BEek](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e1e8f3c4f5144661a34dc856d1917eeb38f04dc0.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2019 21:18 UTC](https://discourse.nodered.org/t/chat-server-ajax-client-side/9543/13 "2019-03-28T21:18:08Z")

</div>

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.
