Node.js+Socket.io:模板@服务器还是浏览器?通过 ajax 或 socket.io 加载内容?
我已经问过一个类似的问题,但这个问题有点不同/具体:
我即将开始开发一个社交社区网站(针对本地用户组),其功能包括时间轴、即时消息/聊天、论坛等……
节点后端的 .js 和 socket.io(或 now.js)。前端的 jQuery(也许还有backbone.js 或类似的)。内容通过 socket.io 或 ajax 加载,并通过 url 哈希进行导航。
有两件事我无法决定走哪条路。我希望这里有一些人可以提供一些好的或坏的经验。
在服务器上还是在浏览器中建立模板?我不确定加载完整的 html 站点 + 时间线、论坛帖子、IM/聊天等的实时更新(也在 html 中)是否更好,或者通过 ajax 或 socket.io 使用 REST api 之类的东西并执行客户端站点上的模板。我以前从未这样做过。您需要下载模板等。有人有这方面的经验吗?还有 2 种方法可以实现类似于 Rest 的 api:例如,请求论坛帖子,然后请求与该帖子关联的用户等等(就像服务器端 MVC) - 或者 - 请求论坛帖子,服务器会回答所有问题需要的信息。
通过ajax或socket.io加载内容?我明确使用 socket.io 或 now.js 进行实时通信(IM、聊天)和 pubsub(在主页上 -> 订阅新的时间线更新,在论坛主题上 -> 订阅新帖子)。但我是否还应该通过套接字加载 HTML(或提供类似 REST 的 API,请参阅问题 1)?当人们在选项卡中打开论坛帖子时(我通常经常这样做),这将意味着大量的套接字连接。而且我不确定 websocket 建立连接需要多长时间。
因此,有 4 种方法可以做到这一点:
- 通过 AJAX 的 HTML - 可能是最稳定的方法,不需要大量 javascript 来进行模板化 - 浏览器可以使用开放的 HTTP 连接来请求内容。
- 通过 socket.io 的 HTML - 必须建立 websocket 来通过
- AJAX 加载内容(可能会更慢) API - 因为它可能需要更多的请求,因为通过 AJAX 的 HTML 可能会有一些 HTTP 标头开销 + 您需要在每个请求中进行身份验证 - 我我不喜欢太多 ajax 请求。
- 通过 socket.io 的 API - 套接字只能进行一次身份验证,并且您可以动态请求 API 对象。不过我仍然会通过 HTTP 加载模板和 js 以进行浏览器缓存。
我知道这是一篇很大的文章,但我已经争论了很多天,只是无法决定,因为一旦开始开发,切换系统将需要大量工作。这不是一个公共项目,它仅限于约 10k-15k 当地人,因此不一定那么完美,在我看来这是一个学习新事物的好机会(我对节点完全陌生,经典的 PHP MVC + jquery dev 在这里)。
I already asked a similar question but this one is a bit different/specific:
I'm about to start development of a social community site (for a local user group) with features like timeline, IM/chat, forums, ...
Node.js and socket.io (or now.js) on the backend. jQuery (and maybe backbone.js or similar) on the front end. Content is loaded via socket.io or ajax and navigation via url hash.
There are 2 things where I just can't decide which way to go. I hope here are some people who can provide some good or bad experience.
Templating on server or in browser? I'm not sure if it's better to load a complete html site + live updates (also in html) for timeline, forum posts, IM/chat, ... or use something like a REST api via ajax or socket.io and do the templating on the client site. I've never done that before. You need to download the templates, etc, etc. Has anyone experience in this? There are also 2 ways to implement a rest-like api: E.g. request a forum post, then request the user associated to that post and so on (just like server side MVC) - or - request a forum post and the server answers with all needed information.
Load content via ajax or socket.io? I'm definitively using socket.io or now.js for real-time communication (IM, chat) and pubsub (on mainpage -> subscribe to new timeline updates, on a forum topic -> subscribe to new posts). But should I also load HTML (or provide a REST-like API, see question 1) through the socket? When people open forum posts in tabs (which I usually do a lot) that would mean a lot of socket connections. And I'm not sure how long it takes for a websocket to establish connection.
So there a 4 ways to do this:
- HTML via AJAX - probably the most stable way that doesn't need a lot javascript to do the templating - Browser can use open HTTP connections to request stuff.
- HTML via socket.io - The websocket must be established to load content (may be slower)
- API via AJAX - as it probably needs more requests as HTML via AJAX there might be some HTTP header overhead + you need to authentication in each request- I'm not a friend of too many ajax requests.
- API via socket.io - Socket must only be authenticated only once and you can request API objects on the fly. However I would still load templates and js via HTTP for browser caching.
I know this is a huge post but I'm debating for many days now and just can't decide as it would be a lot of work to switch the system once started developing. This is not a public project, it's limited to ~10k-15k local people and thus must not be that perfect, a good opportunity to learn new things in my opinion (I'm completely new to node, classic PHP MVC + jquery dev here).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该在后端使用 RESTful api,让模板仅在前端发生(也许使用 Backbone),并且仅将 Socket.IO 用于真正的实时内容(例如聊天)。使用 websockets 来加载 HTML 之类的事情没有任何意义,因为它很可能永远不会改变。
所以我的投票是:
1) HTML via AJAX
2) 通过 AJAX 的 API
3) 实时通信,例如通过 Socket.IO 进行聊天消息(或其他不断变化的内容)
I think you should use a RESTful api on the backend, let the templating occur just on the frontend (maybe with Backbone) and only use Socket.IO for real realtime stuff (such as chat). It doesn't make any sense to use websockets for something like loading HTML, because it most likely never changes.
So my vote is:
1) HTML via AJAX
2) API via AJAX
3) Realtime communication, such as chat messaging (or other stuff that constantly changes) via Socket.IO
尽管确实没有明确的答案,但这取决于情况。
如果您需要可供搜索引擎抓取,则不能仅依赖客户端处理。如果您的个人视图很轻,和/或您需要支持移动设备,则应该在服务器端进行初始渲染。
目前,我建议使用客户端应用程序和服务器端都可以使用的 API。如果您使用 Node 进行服务器端渲染,您可以重复使用许多相同的逻辑,包括 API 客户端。
更进一步,如果您从 github 上的 Yahoo Flux 示例项目开始,您可以在客户端和服务器端使用相同的逻辑,包括使用 React 视图进行渲染。这不是一个简单的解决方案,需要一些工作。
对于交互式元素,服务器端渲染可以最小化,当客户端启动聊天/即时消息时,您的商店通过 sockjs/socket.io 推送事件连接。
当涉及到跨多个进程运行时,您将遇到可扩展性问题,并且可能需要由数据库支持的发布/子链来实现更长的重新连接周期或错过 IM 消息。没有灵丹妙药。
现在,我喜欢flux+react……当Angular2出来时,它可能会有更好的服务器端渲染故事。
Though there really isn't a definitive answer, as it depends.
If you need to be search engine crawlable, you can NOT rely only on client-side processing. If your individual views are light, and/or you need to support mobile, you should have initial rendering server-side.
Currently, I would suggest using an API that both your client application and server-side can use. If you use node for the server-side rendering you can re-use a lot of the same logic, including the API client.
Going a few steps farther, if you look starting with the Yahoo flux examples project on github, you can use the same logic both client and server-side including rendering with React views. This is not an easy solution, and will take some work.
For interactive elements, server-side rendering can be minimal with your stores pushing an event wiring up via sockjs/socket.io when the client starts for your chat/im bits.
You will have scalability issues when it comes to running across multiple processes and will likely need a pub/sub chain backed by a db for longer re-connect cycles or missed IM messages. There isn't a magic bullet.
Right now, I like flux+react... When Angular2 comes out, it may have a better story for server-side rendering.