Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Socket.io and RESTFul work together?

(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 it's actually URL http://example.com/ and some data action=post&content=blahblah.

If I want to post, but not refresh the whole web page, I can use javascript's XMLHTTPRequest. I post it and then get it's content and insert it to a div in my page. These action is all asynchronous.

Then I know there is something named WebSocket and it's wrapper socket.io. It use "message" to communicate between client and server. When I click "post" the client just call socket.send(data) and wait for server's client.send(data). It's magical. But how about URL?

It's possible to use the two model both without repeating myself? In other word, every action has it's URL, and some of them can interact with user real-timely(by socket.io?)

Moreover, should I do this? In a very interactive web program(ex. games), the RESTFul is still meaningful?

like image 667
Lai Yu-Hsuan Avatar asked Jun 14 '11 05:06

Lai Yu-Hsuan


People also ask

How does Socket.IO work internally?

A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving: A session id that will be given for the transport to open connections. A number of seconds within which a heartbeat is expected ( heartbeat timeout )

How do you usually interact with rest APIS?

Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.

Can you use Socket.IO with Express?

Learn how to install and use Socket.io with Express using the npm package manager, and create a simple chat server to see the basics of how a client and server work together. Requirements: You have an account and are logged into the Scaleway console. You have a Scaleway Instance running Ubuntu Xenial or a later version.


1 Answers

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 
like image 129
Josh Avatar answered Oct 16 '22 06:10

Josh