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.

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to use the http module for socket.io or can I use express?
For socket you need install socket.io and for express you need it express
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.