2

So I have a nodejs application which uses socket.io and expressjs

I use port '3000' for the express app and port '8080' for the socket app

  1. Is it possible to use the same port for both these services?(express and socket.io)

  2. When i want to connect to a socket from the client, I use the following code:

    var socket = io('http://localhost:8080')

whats the right way of connecting to it( I see various ways in tutorials across the internet) and have no clue.

Satonamo
  • 291
  • 1
  • 8
  • 17

1 Answers1

0

Is it possible to use the same port for both these services?(express and socket.io)

yes

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

server.listen(8080); //or 3000

When i want to connect to a socket from the client, I use the following code:

At the front end :

include socket.io lib

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080'); //or 3000
</script>

EDIT : without express

var app = require('express').createServer();
var io = require('socket.io')(app);

app.listen(8080);

For more info socket.io

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • Do I need to use the http module for socket.io or can I use express? – Satonamo Jan 19 '17 at 09:20
  • For socket you need install socket.io and for express you need it express – RIYAJ KHAN Jan 19 '17 at 09:21
  • var server = require('http').createServer(app); var io = require('socket.io')(server);. I meant that. Do I need to require the http module ? It can I use 'var up=require ('socket.io').(app) – Satonamo Jan 19 '17 at 09:23