How can Socket.io and RESTFul work together?

前端 未结 2 1660
星月不相逢
星月不相逢 2020-12-04 10:38

(I\'m not familiar to RESTFul, please correct me if my concept is wrong)

In RESTFul architecture, we map every action to an URL. If I click \"post a article\", may i

相关标签:
2条回答
  • 2020-12-04 11:06

    You're defining a handler for actions that map to REST over http. POST and GET generally refer to update and query over an entity. There's absolutely no reason you can't just define a handler for generic versions of these CRUD operations that can be used in both contexts. The way I generally do this is by introducing the concept of a 'route' to the real-time transport, and mapping those back to the same CRUD handlers.

    You have a session, you can impose the same ACL, etc.

     +---------------------------------+
     |                                 |
     |      BROWSER                    |
     |                                 |
     +--+--^-------------------+---^---+
        |  |                   |   |
        |  |                   |   |
     +--v--+---+            +--v---+---+
     |         |            |          |
     | HTTP    |            | SOCKET.IO|
     +--+---^--+            +--+---^---+
        |   |                  |   |
     +--v---+------------------v---+---+
     |                                 |
     |        ROUTING/PUBSUB           |
     +-+--^-------+--^-------+--^------+
       |  |       |  |       |  |
     +-v--+--+  +-v--+--+  +-v--+-+
     |       |  |       |  |      |
     | USERS |  | ITEMS |  |ETC   |
     +-------+  +-------+  +------+
         ENTITY CRUD HANDLERS
    
    0 讨论(0)
  • I posted this on my blog recently:

    Designing a CRUD API for WebSockets

    When building Weld, we are using both REST and WebSockets (Socket.io). Three observations on WebSockets:

    1. Since WebSockets are so free-form, you can name events how you want but it will eventually be impossible to debug.
    2. WebSockets don’t have the request/response form of HTTP so sometimes it can be difficult to tell where an event is coming from, or going to.
    3. It would be nice if the WebSockets could fit into the existing MVC structure in the app, preferably using the same controllers as the REST API.

    My solution:

    • I have two routing files on my server: routes-rest.js and routes-sockets.js
    • My events look like this example: "AppServer/user/create".
    • I use forward slashes (“/”) to make the events look like routing paths.
    • The first string is the target (~”host name” if this actually was a path).
    • The second string is the model.
    • The third string is the CRUD verb: i.e. create, read, update, delete.
    0 讨论(0)
提交回复
热议问题