如何从 Nest 发送通知给客户

huangapple go评论106阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 17:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定