Skip to content

socket.io

Melissa Stock edited this page Jun 5, 2019 · 4 revisions

Overview

  • socket.io is a JS library for real-time web applications (ie: messengers, push notifications, online games)
  • It allows for two way communication between web clients and servers

Getting started

const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http); // Creates new socket instance connected to the http server

  • use socket.emit method to create a custom event
  • use io.socket.emit method to broadcast an event to all clients
  • namespace = path or endpoint

var nsp = io.of('/my-namespace');
nsp.on('connection', function(socket) {
console.log('someone connected');
nsp.emit('hi', 'Hello everyone!');
});

  • use join method to connect a socket to a channel
  • best way to debug: DEBUG=* node app.js

Clone this wiki locally