xjq233p_1
xjq233p_1

Reputation: 8060

Socket.io connection url?

I have the current setup:

  1. Nodejs Proxy (running http-reverse-proxy) running on port 80.
  2. Rails server running on port 3000
  3. Nodejs web server running on port 8888

So any request starting with /nodejs/ will be redirected to nodejs web server on 8888.

Anything else will be redirected to the rails server on port 3000.

Currently Socket.io requires a connection url for io.connect.

Note that /nodejs/socket.io/socket.io.js is valid and returns the required socket.io client js library.


However, I am not able to specify connection_url to /nodejs/ on my server.

I have tried http://myapp.com/nodejs and other variants but I am still getting a 404 error with the following url http://myapp/socket.io/1/?t=1331851089106

Is it possible to tell io.connect to prefix each connection url with /nodejs/ ?

Upvotes: 49

Views: 96203

Answers (5)

Fillipo Sniper
Fillipo Sniper

Reputation: 439

If you are serving your app with express, then maybe you can check this out. Remember express uses http to serve your application.

const express   = require('express'),
      http      = require('http'),
      socketIo  = require('socket.io'),
      app       = express()

var server = http.createServer(app);
var io     = socketIo(server);

io.on('connection', (socket)=>{
      // run your code here
})

server.listen(process.env.PORT, ()=> {
    console.log('chat-app inintated succesfully')
})

Upvotes: 1

zer0Id0l
zer0Id0l

Reputation: 1444

If you are using express with nodejs:

Server side:

var io = require('socket.io')(server, {path: '/octagon/socket.io'});

then

io.on('connection', function(socket) {
        console.log('a user connected, id ' + socket.id);
        socket.on('disconnect', function() {
            console.log('a user disconnected, id '  + socket.id);
        })
})
socket.on('publish message ' + clientId, function(msg) {
        console.log('got message')
    })

Client side:

var socket = io('https://dev.octagon.com:8443', {path: '/octagon/socket.io'})

then

socket.emit('publish message ' + clientId, msg)

Upvotes: 13

supershnee
supershnee

Reputation: 1276

As of Socket.io version 1, resource has been replaced by path. Use :

var socket = io('http://localhost', {path: '/nodejs/socket.io'});

See: http://blog.seafuj.com/migrating-to-socketio-1-0

Upvotes: 66

Neo
Neo

Reputation: 123

I use below approach to achieve this goal:

client side:

var socket = io.connect('http://localhost:8183/?clientId='+clientId,{"force new connection":true});

server side:

var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
    console.log("url"+socket.handshake.url);
    clientId=socket.handshake.query.clientId;
    console.log("connected clientId:"+clientId);

});

reference:https://github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization

Upvotes: 3

Sirian
Sirian

Reputation: 866

you can specify resource like this:

var socket = io.connect('http://localhost', {resource: 'nodejs'});

by default resource = "socket.io"

Upvotes: 13

Related Questions