dongqiang8683 2013-12-23 07:53
浏览 41
已采纳

NodeJS与PHP应用程序和用户会话

I have a PHP application (with Symfony2) and I need to let my users talk, and other tings, in real time (with socket.IO). Let's focus on the chat mechanism : A logged user can talk with an other logged user (logged with FOSUserBundle in Symfony). When the user sent a message, it must be saved in MySQL and sent in real time to the other user. So the message is linked to two users (a sender and a receiver) in my MySQL database.

So I have two possibilities :

  • I use PHP to store messages :
    • I have an event on the click "submit" and call a PHP url with AJAX
    • If my PHP return "OK" (so he correctly added the message in database), I emit a Socket.IO event -> let doctrine deal with datas and symfony with my user
    • On NodeJS side, I have a listener on this event and I sent the message through Socket.IO with an other event
  • I use only NodeJS :
    • How can I log my user on PHP and NodeJS side ?
    • I need to use a different database for datas in real time ?
    • How can I share my users between NodeJS and PHP ?

I don't know what is the cleanest solution, if someone can help me. Thanks !

  • 写回答

1条回答 默认 最新

  • dqsvf28682 2013-12-23 08:13
    关注

    Can be done using a common data store like redis to store user sessions

    Check this link for reference

    http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/

    Also check

    http://www.slideshare.net/leeboynton/integrating-nodejs-with-php-march-2013-1

    Which gives you an overview of how sessions can be shared using a common in-memory data store and some sample memcached code.

    WebSockets

    If you are using nodejs just for the WebSockets part, I'd suggest not to because then you'd have to replicate your user authentication logic in nodejs as well

    You can instead do WebSockets in PHP using some library like http://socketo.me/

    As far as storing the messages in MySQL goes

    Your first approach of making an AJAX call to store the message in database, waiting for the response and then emitting a SocketIO event would lead to a sluggish chat experience. I'd suggest you store messages to MySQL through NodeJS (asynchronously)

    On a side-note check https://github.com/kyle-dorman/ChitChat.js for adding real-time chat functionality to your website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作