I'm attempting to develop a social network that implements publisher-subscribe pattern (kind of like Twitter does): users can follow people, therefore being notified when a new publication of their followers is sent.
All I have now is a working REST service implemented with JaxRS, running over Tomcat 7, offering services for login, register, getting profiles data and submitting posts. Servlet Mapping is done via web.xml
But the weight of the application comes from the pubsub part. And here is where things become messy.
Before speaking of technologies and implementation, the life cycle of this pubsub:
Let be A and B some users of the app and A follows (is subscribed to) B.
- A navigates to his "timeline", therefore registering a WebSocket endpoint in the server from which he will receive notifications when any of the people he's suscribed to publishes anything. (A, himself, would be the topic?)
- B publishes a new post, it's sent to JaxRS service via HTTP POST /post.
- Server stores publication in database and then sends it to listening subscribers via WebSocket. As A is alive and listening, the publication is sent through his WebSocket/topic.
- A gets notified of the new publication.
I've worked with Java built-in WebSockets (javax.websocket), Spring websockets (via STOMP) and JavaScript either built-in and SockJS. I know JaxRS has something called Server Sent Events that act like websockets but I'm absolutely lost on how to initialize and work with them.
My doubts:
1) Could I "mix" e.g Spring Websockets with my JaxRS server and how would it be initialized?
2) With JaxRS and SSE, can I dynamically create WS endpoints on the server?
Thanks in advance
Finally made a workaround using Jersey's Server Sent Events, though not supported by IE, but works for what I needed
Here is the code for the SSEProvider and SSEDispatcher
And front end connection and handlers
Also some docs on HTML5 SSE
来源:https://stackoverflow.com/questions/29785258/integrating-jaxrs-rest-service-with-websockets