1

I am very new to socket Io, node and angular. I am currently working on namespace but I can't really connect to the other namespace. Could you please have a look at what am I doing wrong?

Server.js

const express = require('express')
const app = express();
const path = require('path');
const http = require('http').Server(app);
const io = require('socket.io')(http);

//namespace
var nsp = io.of('/nsp');


//when nsp connected
nsp.on('connection', (socket) =>{
       console.log('nsp connected');

 //when nsp disconnected
 nsp.on('disconnect', function(){
       console.log('nsp disconnected');
        });
    });

//route for nsp
app.get('/nsp', (req, res) => {
    res.send(console.log('Hello'));
});

App.component.ts

import { Component, OnInit } from '@angular/core';
import * as io from 'socket.io-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent{
  nsp;

  constructor() {
    this.nsp = io('http://localhost:3000/nsp');
  }

if a user access to http://localhost:3000/nsp then console should prints nsp connected that i have declared in nsp.on('connection',(socket) =>{console.log('nsp connected');

2
  • It looks like your route is set up on /api/nsp not /nsp Commented Sep 1, 2018 at 2:19
  • @jay can you tell me how can u done this socket code in angular and node bcoz i dont have any idea about this which site u refer and how to do socket programming in angular and node ? Commented Sep 28, 2018 at 14:56

1 Answer 1

1

Since your route is as /api/nsp and not just /nsp your code in App.component.ts should be as follow :

constructor() {
    this.nsp = io('http://localhost:3000/api/nsp');
  }
Sign up to request clarification or add additional context in comments.

5 Comments

Silly me. Thanks a lot
Please accept the answer so it will help others also
Man, turns out that was not my actual concern.. can you have a look at my code one more time?
Changing your question after an answer is not right bro.. if you have another question you have to ask again rather than completely changing the question
Sorry bro I will repost my quesion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.