简体   繁体   English

使用Socket.IO和PHP推送通知

[英]Push notifications using Socket.IO and PHP

The idea/context: 想法/背景:

I'm thinking about giving my users a nice little extra feature: I want to add push notifications. 我想给我的用户一个不错额外的功能:我要添加推送通知。 This is the use case: 这是用例:

People have a guestbook at their profile page. 人们在个人资料页面上有留言簿。 When someone posts a message in a user's guestbook, that user will receive a push notification (if he's online ofcourse). 当有人在用户的留言簿中发布消息时,该用户将收到推送通知(如果他在线的话)。 If he's not online, the next time he comes online, we'll just pull the notifications from the DB. 如果他不在线,下次他上网时,我们只会从数据库中提取通知。

I was thinking about doing this with Socket.IO running on a Node.JS server. 我正在考虑使用在Node.JS服务器上运行的Socket.IO来做这件事。 My current application is built with PHP (so the posting etc. is handled by PHP). 我当前的应用程序是用PHP构建的(所以发布等由PHP处理)。

All online users will connect using Socket.IO to listen for their own notifications. 所有在线用户都将使用Socket.IO进行连接,以收听他们自己的通知。 Their socket will be saved in an array or hash on the server. 它们的套接字将保存在服务器上的数组或哈希中。

This is the flow I have in mind: 这是我想到的流程:

  1. UserA posts a message in guestbook of UserB UserA在UserB的留言板中发布消息
  2. Make Socket.IO emit a notification to UserB (if online, so known by Socket.IO) 让Socket.IO向UserB发出通知(如果在线,Socket.IO已知)
  3. Save the message in DB 将消息保存在DB中

The issue here is the ' make Socket.IO emit a notification '-part. 这里的问题是' make Socket.IO发出通知 '-part。 I would need a way to do this from PHP, because I want the server to emit this notification and not the user that is posting the message. 我需要一种方法来从PHP执行此操作,因为我希望服务器发出此通知而不是发布消息的用户 Why you ask? 你为什么问? I want to prevent malicious users from creating fake notifications. 我想阻止恶意用户创建虚假通知。 So in pseudocode the PHP application would look like this: 所以在伪代码中PHP应用程序看起来像这样:

// do some validations here ...

// This is the message that was posted
$message = array(
    'from' => 'UserA',
    'to' => 'UserB',
    'msg' => 'Hello you'
);

// Send a notification to the user by emitting an event
socketio_emit('notification', json_encode($message));

save_in_db($message);

The question(s): 问题:

What are your thoughts about this? 你对此有何看法? Are there better ways to implement this seemingly small feature? 是否有更好的方法来实现这个看似很小的功能? And also, how would I do the socketio_emit() in PHP, in other words: how do I communicate with a Socket.IO server using PHP? 而且,我如何在PHP中执行socketio_emit() ,换句话说:如何使用PHP与Socket.IO服务器通信?

Thanks a lot! 非常感谢!

I solved this by using Express.js and CURL to post new notifications. 我通过使用Express.js和CURL发布新通知来解决这个问题。 The Node.js server listens to a specific URL, eg /new_notification. Node.js服务器侦听特定的URL,例如/ new_notification。 Doing a POST request from my webserver with CURL to that URL, I can add new notifications and handle them with Socket.IO (all in the same Node.js application). 从我的网络服务器使用CURL向该URL发出POST请求,我可以添加新通知并使用Socket.IO处理它们(所有这些都在同一个Node.js应用程序中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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