Last Updated: February 25, 2016
·
2.25K
· pmaoui

Using Socket.io and Express 3.X on the same port

I'm using CoffeeScript. In my opinion, it should replace javascript :)

If you don't think so, you can convert that code to plain javascript here : http://js2coffee.org/

 First we have some dependencies :

express = require 'express'
io = require 'socket.io'
http =  require 'http'

With Express 3.X you have to explicitely use http and create your server :

server = http.createServer app
io = io.listen server
io.set 'log level', 1
server.listen 80

 Now you can manage all your connections :

io.sockets.on 'connection', (socket)->
    address = socket.handshake.address
    client_ip = address.address
    socket.on 'hello',->
        socket.emit 'hello_you',
            you_are : client_ip

Have a fresh tip? Share with Coderwall community!

Post
Post a tip