Cover image for You don't need express to get started with socket.io

You don't need express to get started with socket.io

sadick profile image Sadick ・1 min read

The best place to get started with socket.io is their website. But once you access it you are given this example app demonstrating how to use socket.io.

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

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');

io.on('connection', function(socket){
  console.log('a user connected');

http.listen(3000, function(){
  console.log('listening on *:3000');

If you are fairly new to node, this makes you think that express is needed in order to use socket.io. In the above example they include express to serve the index file which will be client app for your socket server. There is nothing wrong with the above approach but if lets say you don't know express, now you have one more thing to learn before using socket.io.

It would be better if they didn't introduce an additional library in their example. Socket.io has another example using the node http server

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');


function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      return res.end('Error loading index.html');


io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {

But even this is a little bit too much. I prefer having the server and the client seperate. Therefore this would be my server.

const io = require("socket.io");
const server = io.listen(3000);

server.on("connection", function(socket) {
  console.log("user connected");
  socket.emit("welcome", "welcome man");

And the client would include the socket.io client library and just plain html.

        <title>Socket io client</title>
        <script src="http://localhost:3000/socket.io/socket.io.js"></script>
            var socket = io("http://localhost:3000");
            // use your socket
            socket.on("welcome", (message) => {
                // do something with the message.

Posted on by:

sadick profile



Programming is more about thinking than typing


Editor guide

And most important, you do not need socket.io to start with websockets.


Its true, you don't. But socket.io implements WebSocket backend and frontend with many fallbacks for probably every possible browser you can imagine.


I know but you do not need that when you get started with real-time communication, which I presumed the topic is all about.

I'm pro learning the base technology first (vanilla web socket) then learn socketIO (which solve a few problems you will encounter in production).


I think this generally isn't relevant anymore. Even IE implements websockets.

IE implements Websockets from version 11. It only becomes irrelevant if you don't intend to support versions 10 and below.

Not even Microsoft supports IE 10 and below anymore. If your company is supporting it, they need to look back at their policies because supporting a browser that hasn't been supported by its vendor since 2016 is a security risk.


And what's up next? Are there any special options to control events on the client-side?


Socket.io has a client api which specifies the options available.


imho, I think, it will be better to provide example

Sure. I will include an example on the post and notify you once its up.


I have included a piece on the client on how to consume the socket io event emitted from the server. Is it sufficient?


What about doing the same thing with https ?


Thanks man. Very nice.


how can I send msg only room member?
"socket.to(roomName).emit('joinRoom2', roomName, name); "is not working


Great. How could it be implemented with https?


Also, Test/debug the socketio events while you're developing it without writing a single line of frontend code. (no it's not console.log)

It's firecamp.app


Any one can help to develop a socket IO communication between local server and web server to sync data. Pls send me mail at skumarrcg@gmail.com or whatsapp me at +60173051009