Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

node.js - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...