Angular2/Sails : How to access global io.socket object from Angular2 component?

439 views
Skip to first unread message

Mike Kelly

unread,
May 12, 2016, 2:36:24 PM5/12/16
to sails.js

Hello,


I’m working on an Angular2/Sails.js solution that involves real time communication through the use of the Sail’s socket client.  


This may be more of an Angular question but thought I would ask in the event others have insight.

 

In index.html, I’ve verified that the global io.socket object is available and functional as a result of including these two scripts.

 

<script type"text/javascript" src="node_modules/socket.io-client/socket.io.js"></script>

<script type"text/javascript" src="node_modules/sails.io.js/sails.io.js" ></script>

 

The challenge I’m faced with granting my Angular2 service component access to the global io.socket object.  


I think the right solution is to add a global socket provider to boot.ts so that any of my services have access to the single persistent connection however I've not been successful at connecting the dots.  

 

Boot.ts

bootstrap(AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS, SAILS_CONNECTION);   

 

It’s probably simple, but how do I access the global io.socket object from an Angular class/component?


Thank you in advance.

Mike Kelly

unread,
May 12, 2016, 6:47:10 PM5/12/16
to sails.js
I was able to solve by adding 

declare var io: any;

to my Angular component outside of the class declaration.
Reply all
Reply to author
Forward
0 new messages