英文:
How to send a notification from Nest to a client
问题
我正在开发类似于即时通讯应用的项目。我使用 Nest.js 编写后端。用户将在手机上使用渐进式Web应用(PWA),在浏览器中使用网站。我想要实现一个通知系统,告诉用户有新消息到达,但我不知道如何实现。我不想使用第三方服务。有人能告诉我吗?
在数据库中,我存储了每个用户的所有设备的设备和IP信息。也许可以利用这些信息发送通知?
英文:
I'm developing something like a messenger. I write backend using Nest js. And the user will have a PWA on the phone and a website in the browser. I want to make a notification that a new message has arrived to the user, but I can't figure out how to implement it. I don't want to use any third-party services for this. Can someone tell me ?
In the database, I store the device and ip of each user from all his devices. Maybe using this you can send a notification?
答案1
得分: 0
如果你想要获取“原始”数据,你可以每隔x秒向服务器发送请求,看是否有新的内容。
然而,这并不是一个最优的解决方案。通常在这种情况下,人们使用WebSocket。WebSocket不是第三方工具,并且被所有浏览器支持。
英文:
If you want it "raw", you can just send a request to the server each x seconds whether there is anything new.
This is however not an optimal solution. Usually, in such cases, people use WebSocket. It is not a third-party tool and it is supported by all browsers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论