Skip to content

Instantly share code, notes, and snippets.

@maxmckenzie
Last active August 14, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxmckenzie/dfccfc4bfe31183a37c82da908c9f2aa to your computer and use it in GitHub Desktop.
Save maxmckenzie/dfccfc4bfe31183a37c82da908c9f2aa to your computer and use it in GitHub Desktop.
Simple Socket.io example
// https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.6.0/socket.io.min.js
var socket = io('http://localhost:5050');
socket.on('news', function (data) {
console.log(data);
socket.emit('client event', { my: 'data' });
});
import express from 'express';
import http from 'http'
/**
* Express Set Up
*/
const app = express()
app.set('view engine', 'pug')
app.use('/assets', express.static('assets'));
const server = http.createServer(app);
const io = require('socket.io').listen(server);
server.listen(5050);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment