Reputation: 203
I want my socket connection(socket.io) on the webapp (yeoman) has the following functions:
When non-logged-in user visit the web app. The default setting of the client socket is listening to '/welcome' namespace. When user logged in the webapp, the client socket will change to listen to another namespace ('/home'). Then when the user logged out, the client socket will change back listen to '/welcome' namespace.
Library:
socket: socket.io
yeoman: yeoman-angular-fullstack
Upvotes: 0
Views: 127
Reputation: 2757
I do it that way:
upon rendering a certain template I (via middleware on server side) pass one additional parameter to view like isLoggedIn
, which, of course it true when user is logged in.
Inside view template I create a hidden dom element and assign to it 'isLoggedIn" value.
Inside client's js file before websocket connection I read p2 hidden DOM value to understand what I have to do next. P2 & p3, unfortunately, is the only way I know to normally assign value to JavaScript variable on client side that is passed to view. You can, of course, do it via API call, but whatever.
Depending on this variable connect either to /welcome
or to /home
via simple if
statement.
Upvotes: 0