1

This is not a straight question but rather a call for opinion.

I am in the process of designing an API using NodeJS and I would really need sockets for some applications but not for all.

Is it good practise to mix both Socket.IO and normal Express REST API point ? what are your opinions ? What would be the advantages and disadvantages to do the mix up ?

0xtuytuy
  • 1,494
  • 6
  • 26
  • 51
  • It's not clear what you mean by "mix both Socket.IO and normal Express REST API point". One should use socket.io for what it is best for and REST APIs for what they are best for. If you have a need for both mechanisms, then use both tools. – jfriend00 Jan 01 '18 at 19:35
  • Well rather then only calling the API using sockets, having more traditional REST API points that accept POST and GET requests – 0xtuytuy Jan 01 '18 at 19:36
  • There is no generic answer to that. Depends upon the specifics of what you're trying to build. In general, request/response things should be done with REST API because socket.io is not request/response architecture. But, it really depends upon details of what you're trying to do which you have not explained at all. As it stands now, there's not enough info here to offer any advice so the question is too general. – jfriend00 Jan 01 '18 at 19:38
  • What would be the things that I need to consider to help me make a decision ? – 0xtuytuy Jan 01 '18 at 19:40
  • A few references: [Websocket vs REST when sending data to server](https://stackoverflow.com/questions/45460734/websocket-vs-rest-when-sending-data-to-server/45464306#45464306) and [Ajax vs Socket.io](https://stackoverflow.com/questions/30319618/ajax-vs-socket-io/30334848#30334848) – jfriend00 Jan 01 '18 at 19:55

1 Answers1

2

It's entirely fine to use HTTP endpoints and websockets, as until HTTP/2 came along, the only solutions to getting updates back from background processes were:

  1. Polling
  2. Callbacks (Web Hooks)

Neither of these are ideal, but luckily HTTP/2 with Server Push means you can stream down updates as they come, making WebSockets not quite so required.

That said if you have other use cases for WebSockets then absolutely, off you go!

Relevant: https://www.infoq.com/articles/websocket-and-http2-coexist

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117