0

I never done any API, I just recently become aware of REST, never used sockets or node.js, but I have this simple project in mind using all of these.

Imagine usual app with request/response stuff. Nothing fancy. But then sometimes I need real time functionality, lets say there's a live support for website, a chat. So majority of users never need sockets and everything is easy, but when they do, what's then? How that would look and work with restful api?

Erndob
  • 2,512
  • 20
  • 32

1 Answers1

1

As you tag, socket.io is perfect for you. It creates a socket within the browser to your server without the user installing any third party program, using websockets and longpolling. And for the users that have old browsers and don't have those browser built-in functions, it can fallback to a third party plugin: Flash Player, but almost all browsers have it installed.

Is you are used to Javascript or object oriented programming, socket.io and node.js is a walk in the park. If you don't want to use node.js and socket.io, you can write your own implementation of client-server with this info:

WebSockets
Long Polling example
Flash AS3 Socket

As a small adition, simply you need your default web server (Apache, Nginx, Lighthttpd, whatever...) running in default port 80 and also running a node.js server in other port, let's say 8080. That second server will serve all the files needed to connect, because socket.io can only connect to the same domain and port that served the files (security reasons, I guess).

In short, you'll have 2 servers: One serving your entire webpage and another one serving the files needed to connect to your chat (and also serving the chat, obviously).

I have exactly that configuration made in one of my pages (a live sports streaming site) and to add the chat to my site I have this server running in port 8080 and I load it in the main page inside an iframe: http://www.example.com:8080/

As an adition, you can create a complete http server in node.js, but I don't guess that it is useful as a professional web server.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
  • How would URI in Restful api would look like for this? If I want to open socket between two users to chat? Something like api.domain.com/chat/users/{user_id} ? also its not get/put/delete/post because its continuous connection, how to approach this from rest side? Or I need to forget REST when I think about sockets? – Erndob Sep 17 '15 at 11:38
  • If you want sockets with realtime notifications, you should forget REST. In the other hand, with REST you simply have to do a "loop" that will ask the server for new messages each X seconds, but that creates overload for no reason (most of the times the browser will be asking for new messages with empty replies), and also messages wont be 'realtime/0 latency' because users will receive a new chat message only when the loop asks for new messages. This leads to another question, the faster the loop asks, the more realtime the messages will be, but the more overload you create on the server. – Jorge Fuentes González Sep 17 '15 at 11:46
  • 1
    I searched a bit more and there are some implementations around there, to make rest work with `socket.io`: http://stackoverflow.com/questions/6339393/how-can-socket-io-and-restful-work-together so you question is perfectly viable. – Jorge Fuentes González Sep 17 '15 at 11:52