Friday, 26 May 2017

socket.io with express 4.15 template from WebStorm not working

I'm trying to setup a Node.js server with socket.io. I'm using the latest template given by WebStorm template.

My step is first add socket.io to my languague & framework - Node.js and NPM. Here is my setting

enter image description here

Then in my "bin/www" I add the bellow

var io = require('socket.io')(server)

And add this to bottom of the bin/www file (after server has start listening)

io.on("connection",(socket)=>{
  console.log("user connected" + socket.id)
})

Then I modify my index.jade as bellow

extends layout

block content
  h1= title
  p Welcome to #{title}

  li what the ul1
  li what the ul2
  script.
    src="/socket.io/socket.io.js"
  script.
    var socket = io();

Run the server. visit the page and this is what i get

enter image description here

I get the error

Uncaught ReferenceError: io is not defined
    at localhost/:1

Anyone know what wrong with my implementation. Help is much appreciate. thanks!



via Lê Khánh Vinh

No comments:

Post a Comment