I have created simple Socket Server in PHP and Make Chat Application with that.
Socket server working on some port e.g. 127.0.0.1:9999
All clients connect with that IP:PORT and Shake-hand to server and communicate with each other.
My Concept:
1) Browser
=> Get Real-time Notifications by ( WebSocket )
2) User
=> Post Comments ( Want to Save Comment in Database either using REST or simple )
After saving my Post to Database It should be broadcast to all users GUI without any HTTP Request
( By Only WebSocket ).
So, My question is regarding, How to handle Second Part when User post comment inside my WebSocketServer
.
I guess what you might have in mind is something like this:
Browser
|
+----> REST/HTTP ---> Web Server (REST)
| |
| (*)
| |
| v
+-<--- WebSocket <--- WebSocket Server
The browser does REST/HTTP requests, and at the same time maintains a WebSocket connection to receive real-time notifications.
The browser does REST/CRUD requests to your Web/REST server, and when the REST server performs a CRUD operation, it not only answers to the requestor, but also sends out notification to others via WebSocket.
There are 2 important aspects:
First, WebSocket isn't enough, since it is only raw point-to-point messaging). You likely want some "Publish & Subscribe" schema on top of WebSocket, so that the CRUD operation on a given resource can send out a notification only to those browser/WebSocket clients that are interested and allowed to receive notification for the respective resource.
Second, depending on the technology of your Web server, you want to make the () as easy as possible (e.g. doing () by a simple HTTP request from your Web server to the WebSocket server).
Both of above is provided e.g. by Crossbar.io:
- Crossbar.io is a WAMP router, and WAMP ("The Web Application Messaging Protocol") provides "publish & subscribe" over WebSocket.
- Crossbar.io has a builtin HTTP-to-WAMP bridge that allows you to publish by doing a plain old HTTP request.
Please see here for an example.
Disclosure: I am affiliated with WAMP and Crossbar.io.