简体   繁体   中英

How to listen the event from server without web-socket

I want to implement one validation feature where the client submits some data like CSV files. And the server will do the validations. Validations are done in multiple stages each stage can take a variable amount of time to validate. Whenever the server completes one validation step, it should send it to the client, so that I can show it to the user in UI.

What should be the better way to do it?

I don't want to use WebSocket, as this is the only feature using that and it will be momentary.

I also don't want to have long polling as it will keep the server busy unnecessary.

I want something like firebase HTTPS polling it keeps open for a while and discards it every 3 sec, and in between keep listening to the server. But I don't know how to implement it.

Also if the answer is a keep-alive HTTPS connection. Not sure how to write code for that either. So the example in React/JS and the node can be very helpful.

You could also use rejax.io api service which you can make http post request to it with a message that will be broadcasted to client(s) for you. I am author.

  • register for a free api key
  • include client script for clients
  • make (from server) http post request to service with message
  • message is broadcasted immediately to clients
  • your server continues with processing

client code:

Rejax.listen('my-channel-name', function(text) {
    // received text from server
    console.log(text)
})

server code:

const axios = require('axios')
axios
    .post('https://rejax.io:3001/api/server', {
        'app_key' : 'MY_APP_KEY',
        'app_secret' : 'MY_APP_SECRET',
        'channel' : 'my-channel-name',
        'text' : 'hello from server'
    })
    .then(res => {
        console.log(res.data)
    })
    .catch(error => {
        console.error(error)
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM