Websocket API to replace REST API?

后端 未结 10 1620
温柔的废话
温柔的废话 2020-12-02 03:43

I have an application whose primary function works in real time, through websockets or long polling.

However, most of the site is written in a RESTful fashion, which

相关标签:
10条回答
  • 2020-12-02 04:04

    Not to say that the other answers here don't have merit, they make some good points. But I'm going to go against the general consensus and agree with you that moving to websockets for more than just realtime features is very appealing.

    I am seriously considering moving my app from a RESTful architecture to more of an RPC style via websockets. This is not a "toy app", and I'm not talking about only realtime features, so I do have reservations. But I see many benefits in going this route and feel it could turn out to be an exceptional solution.

    My plan is to use DNode, SocketIO, and Backbone. With these tools, my Backbone models and collections can be passed around from/to client and server by simply calling a functions RPC-style. No more managing REST endpoints, serializing/deserializing objects, and so forth. I haven't worked with socketstream yet, but it looks worth checking out.

    I still have a long way to go before I can definitively say this is a good solution, and I'm sure it isn't the best solution for every application, but I'm convinced that this combination would be exceptionally powerful. I admit that there are some drawbacks, such as losing the ability to cache resources. But I have a feeling the advantages will outweigh them.

    I'd be interested in following your progress exploring this type of solution. If you have any github experiments, please point me at them. I don't have any yet, but hope to soon.

    Below is a list of to-read-later links that I've been collecting. I can't vouch that they are all worthwhile, as I've only skimmed many of them. But hopefully some will help.


    Great tutorial on using Socket.IO with Express. It exposes express sessions to socket.io and discusses how to have different rooms for each authenticated user.

    • http://www.danielbaulig.de/socket-ioexpress/

    Tutorial on node.js/socket.io/backbone.js/express/connect/jade/redis with authentication, Joyent hosting, etc:

    • http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/
    • http://fzysqr.com/2011/03/27/nodechat-js-continued-authentication-profiles-ponies-and-a-meaner-socket-io/

    Tutorial on using Pusher with Backbone.js (using Rails):

    • http://blog.pusher.com/2011/6/21/backbone-js-now-realtime-with-pusher

    Build application with backbone.js on the client and node.js with express, socket.io, dnode on the server.

    • http://andyet.net/blog/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/
    • http://addyosmani.com/blog/building-spas-jquerys-best-friends/
    • http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/
    • http://fzysqr.com/2011/03/27/nodechat-js-continued-authentication-profiles-ponies-and-a-meaner-socket-io/

    Using Backbone with DNode:

    • http://quickleft.com/blog/backbone-without-ajax-part-ii
    • http://quickleft.com/blog/backbone-without-ajax-part-1
    • http://sorensen.posterous.com/introducing-backbone-redis
    • https://github.com/cowboyrushforth/minespotter
    • http://amir.unoc.net/how-to-share-backbonejs-models-with-nodejs
    • http://hackerne.ws/item?id=2222935
    • http://substack.net/posts/24ab8c
    0 讨论(0)
  • 2020-12-02 04:05

    I'm thinking about transitioning to a WebSocket api for all site functions

    No. You should not do it. There is no harm if you support both models. Use REST for one way communication/simple requests & WebSocket for two way communication especially when server want to send real time notification.

    WebSocket is a more efficient protocol than RESTful HTTP but still RESTful HTTP scores over WebSocket in below areas.

    1. Create/Update/Delete resources have been defined well for HTTP. You have to implement these operations at low level for WebSockets.

    2. WebSocket connections scale vertically on a single server where as HTTP connections scale horizontally. There are some proprietary non standards-based solutions for WebSocket horizontal scaling .

    3. HTTP comes with a lot of good features such as caching, routing, multiplexing, gzipping etc. These have to built on top of Websocket if you chose Websocket.

    4. Search engine optimizations works well for HTTP URLs.

    5. All Proxy, DNS, firewalls are not yet fully aware of WebSocket traffic. They allow port 80 but might restrict traffic by snooping on it first.

    6. Security with WebSocket is all-or-nothing approach.

    Have a look at this article for more details.

    0 讨论(0)
  • 2020-12-02 04:05

    The only problem I can using TCP (WebSockets) as your main web content delivery strategy is that there is very little reading material out there about how to design your website architecture and infrastructure using TCP.

    So you can't learn from other people's mistakes and development is going to be slower. It's also not a "tried and tested" strategy.

    Of course your also going to lose all the advantages of HTTP (Being stateless, and caching are the bigger advantages).

    Remember that HTTP is an abstraction for TCP designed for serving web content.

    And let's not forget that SEO and search engines don't do websockets. So you can forget about SEO.

    Personally I would recommend against this as there's too much risk.

    Don't use WS for serving websites, use it for serving web applications

    However if you have a toy or a personal websites by all means go for it. Try it, be cutting-edge. For a business or company you cannot justify the risk of doing this.

    0 讨论(0)
  • 2020-12-02 04:09

    I learned a little lesson (the hard way). I made a number crunching application that runs on Ubuntu AWS EC2 cloud services (uses powerful GPUs), and I wanted to make a front-end for it just to watch its progress in realtime. Due to the fact that it needed realtime data, it was obvious that I needed websockets to push the updates.

    It started with a proof of concept, and worked great. But then when we wanted to make it available to the public, we had to add user session, so we needed login features. And no matter how you look at it, the websocket has to know which user it deals with, so we took the shortcut of using the websockets to authenticate the users. It seemed obvious, and it was convenient.

    We actually had to spend quiet some time to make the connections reliable. We started out with some cheap websocket tutorials, but discovered that our implementation was not able to automatically reconnect when the connection was broken. That all improved when we switched to socket-io. Socket-io is a must !

    Having said all that, to be honest, I think we missed out on some great socket-io features. Socket-io has a lot more to offer, and I am sure, if you take it in account in your initial design, you can get more out of it. In contrast, we just replaced the old websockets with the websocket functionality of socket-io, and that was it. (no rooms, no channels, ...) A redesign could have made everything more powerful. But we didn't have time for that. That's something to remember for our next project.

    Next we started to store more and more data (user history, invoices, transactions, ...). We stored all of it in an AWS dynamodb database, and AGAIN, we used socket-io to communicate the CRUD operations from the front-end to the backend. I think we took a wrong turn there. It was a mistake.

    • Because shortly after we found out that Amazon's cloud services (AWS) offer some great load-balancing/scaling tools for RESTful applications.
    • We have the impression now that we need to write a lot of code to perform the handshakes of the CRUD operations.
    • Recently we implemented Paypal integration. We managed to get it to work. But again, all tutorials are doing it with RESTful APIs. We had to rewrite/rethink their examples to implement them with websockets. We got it to work fairly fast though. But it does feel like we are going against the flow.

    Having said all that, we are going live next week. We got there in time, everything works. And it's fast, but will it scale ?

    0 讨论(0)
  • 2020-12-02 04:13

    Do I want updates from the server?

    • Yes: Socket.io
    • No: REST

    The downsides to Socket.io are:

    • Scalability: WebSockets require open connections and a much different Ops setup to web scale.
    • Learnin: I don't have unlimited time for my learnin. Things have to get done!

    I'll still use Socket.io in my project, but not for basic web forms that REST will do nicely.

    0 讨论(0)
  • 2020-12-02 04:17

    WebSockets (or long polling) based transports mostly serve for (near) real-time communication between the server and client. Although there are numerous scenarios where these kinds of transports are required, such as chat or some kind of real-time feeds or other stuff, not all parts of some web application need to be necessarily connected bidirectionally with the server.

    REST is resource based architecture which is well understood and offers it's own benefits over other architectures. WebSockets incline more to streams/feeds of data in real-time which would require you to create some kind of server based logic in order to prioritize or differentiate between resources and feeds (in case you don't want to use REST).

    I assume that eventually there would be more WebSockets centric frameworks like socketstream in the future when this transport would be more widespread and better understood/documented in the form of data type/form agnostic delivery. However, I think, this doesn't mean that it would/should replace the REST just because it offers functionality which isn't necessarily required in numerous use cases and scenarios.

    0 讨论(0)
提交回复
热议问题