最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Listener in front end to listen on back end listener - Stack Overflow

programmeradmin0浏览0评论

I’ve been looking for an answer regarding this but can’t find anything good. I have a listener on cloudant which I have built in NodeJS. So when cloudant gets updated a function in my code gets called. My problem is that this data should be available as live data in a front end application. If I put the listener in an API-endpoint/middleware it won’t get called unless it gets a request from front end I guess?

So my question is: How can I create a listener in backend which can send data to front end whenever a change in cloudant appears? Basically I want a listener in front end to listen on a listener in back end.

I’ve been looking for an answer regarding this but can’t find anything good. I have a listener on cloudant which I have built in NodeJS. So when cloudant gets updated a function in my code gets called. My problem is that this data should be available as live data in a front end application. If I put the listener in an API-endpoint/middleware it won’t get called unless it gets a request from front end I guess?

So my question is: How can I create a listener in backend which can send data to front end whenever a change in cloudant appears? Basically I want a listener in front end to listen on a listener in back end.

Share Improve this question edited Jun 26, 2020 at 18:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 27, 2018 at 15:57 danielodanielo 7803 gold badges14 silver badges33 bronze badges 1
  • I think your question would be less localized (and better suited to receive good answers) if you asked how to have the your server-side application notify your client-side application when a change happens on the server side. Because that's what you're really looking to do. Maybe something like signalrjs would help. – JLRishe Commented Jan 27, 2018 at 16:01
Add a ment  | 

1 Answer 1

Reset to default 7

So, what you're looking for is the ability to send or "push" data from server to client. The typical way of doing this is with a webSocket or socket.io connection. The client connects to the server and creates a lasting connection to the server. From then on, the server can just send data to the client over that connection whenever it wants to. The client then creates a listener on that connection so it will know when there is ining data and it can then act accordingly based on the data.

webSocket is the standard built-into browsers that enables this type of function. socket.io is an additional client and server layer built on top of a webSocket connection that adds a lot of useful features such as auto-reconnection if the connection dies, a JSON message definition layer so you don't have to define your own data format, etc...

Here's how this would normally work:

  1. When the server initializes, it creates a socket.io listener for ining socket.io connections. webSocket/socket.io are built to "share" the same web server that you are using for loading web pages so you don't need an additional server or port.
  2. When a page loads in the browser, some Javascript in that page creates a socket.io connection to the server.
  3. The client then sets up listeners for whatever messages it wants to be able to act on.
  4. Meanwhile, when the server gets something that it wants to send to the clients, it can either send that data to all currently connected clients or it can send it to only one specific client.
  5. Then, the client's event listener will trigger and it will receive the data.
  6. The client can then decide what it wants to do with the data, typically insert something in the current displayed page.
  7. When the browser is changed to another web page, the socket.io connection will automatically be disconnected.

The socket.io documentation has several pieces of sample code for both client and server to show you how it is programmed.

发布评论

评论列表(0)

  1. 暂无评论