A recommended way to handle a WebSocket into a Page

Hi all!
I’m struggling a little figuring out the best way to reflect Socket.IO events into a Page and its Modals…

I’ve build a SocketService being injected in the page and into the modals for now, but I wonder is someone have played with stuff of this kind and have any structural recommendation to handle this properly, and send data from the Socket back to the Pages, etc.

Thanks in advance!

I had been struggling with this issue myself. I really need a socket service that I can access from any page and be able to both send and receive socket.io events.

After lots of research, I decided to use the Observable pattern. And here is what I came up with:

Here is the article that helped me really understand Observable and its event handling capaciblities:
http://coenraets.org/blog/2016/02/angular2-ionic2-data-services-promises-observables/

The way the SocketService works is, first it creates an observable instance, and call its next() function on any event that the clients should know about. Then you can inject SocketService into any page and call the subscribe method to be notified of events.

My solution is by no means the best way to do it. If anyone finds a better way, I’d love to hear your solution.

1 Like