Reader small image

You're reading from  Socket.IO Cookbook

Product typeBook
Published inOct 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781785880865
Edition1st Edition
Languages
Right arrow
Author (1)
Tyson Cadenhead
Tyson Cadenhead
author image
Tyson Cadenhead

Tyson Cadenhead works as a senior JavaScript engineer at Aloompa in Nashville, Tennessee. He has dedicated his professional career to building large-scale applications in JavaScript and Node. Tyson addresses audiences at various conferences and programming meetups on how to build real-time web applications with Socket.IO or Meteor.js. He blogs on topics such as JavaScript and web technologies at http://www.tysoncadenhead.com. Tyson lives in the greater Nashville area with his wife and two sons, where he enjoys gardening, raising chickens, reading philosophy and economics books, and playing guitar.
Read more about Tyson Cadenhead

Right arrow

Uploading an image to the filesystem


With Socket.IO, we can send files to our server over WebSockets instead of using an http POST request. Socket.IO allows us to upload files in real time and display the uploaded images as needed.

In this recipe, we will use Socket.IO to upload a file to our local filesystem and then send a message to the client to display the image when it is done uploading.

Getting ready…

For this recipe, we will use the built-in Node fs module to upload our images.

We will upload files to our filesystem, so make sure that you create a folder called tmp in the root of your project.

How to do it…

To upload an image to the filesystem with Socket.IO, follow these steps:

  1. First, we will need to create our server.js file. This file will be responsible for listening for new upload-image messages and uploading the file that is passed with the arguments:

    var express = require('express'),
    app = express(),
    http = require('http'),
    socketIO = require('socket.io'),
    fs = require('fs'),
    path...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Socket.IO Cookbook
Published in: Oct 2015Publisher: PacktISBN-13: 9781785880865

Author (1)

author image
Tyson Cadenhead

Tyson Cadenhead works as a senior JavaScript engineer at Aloompa in Nashville, Tennessee. He has dedicated his professional career to building large-scale applications in JavaScript and Node. Tyson addresses audiences at various conferences and programming meetups on how to build real-time web applications with Socket.IO or Meteor.js. He blogs on topics such as JavaScript and web technologies at http://www.tysoncadenhead.com. Tyson lives in the greater Nashville area with his wife and two sons, where he enjoys gardening, raising chickens, reading philosophy and economics books, and playing guitar.
Read more about Tyson Cadenhead