Zhehao Victor Zhou
Zhehao Victor Zhou

Reputation: 203

Change Socket.io Namespace

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

Answers (1)

kaytrance
kaytrance

Reputation: 2757

I do it that way:

  1. 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.

  2. Inside view template I create a hidden dom element and assign to it 'isLoggedIn" value.

  3. 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.

  4. Depending on this variable connect either to /welcome or to /home via simple if statement.

Upvotes: 0

Related Questions