Build an app like Snapchat



Stories! Stories are everywhere these days. Snapchat, Instagram, Facebook, Facebook Messenger and Whatsapp all have stories. Hell, it’s only a matter of time before Microsoft Excel gets a stories update.

Microsoft Excel Stories

In any case, if you want to build an app like Snapchat or build an app like Instagram, there is an easy way to do so. The focus of this tutorial will be about building the story functionality of Snapchat and Instagram into an app. We’ll save the whole disappearing message thing for a later date.

Okay, so you might be thinking that this is going to be a whole lot of work. Guess what? It’s not. This is all thanks to an amazing open source library on GitHub called Zuck.js.

Zuck.js

Zuck.js is a javascript library that easily allows you to add stories into an app. It already comes ready to handle opening stories, cube animation effects and giving you the clean Instagram or Snapchat look. It really makes building stories super easy to do! So because this is a javascript library, we are going to build a webapp that can run in the phone’s browser. It’s going to be very basic and look something like this:

build an app like snapchat

 

Setting up the HTML

To get started, let’s create a basic HTML file:

<!DOCTYPE html>

<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->

<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->

<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->

<!--[if gt IE 8]><!-->

<html class="no-js">

<!--<![endif]-->

<head>

<metacharset="utf-8">

<metahttp-equiv="X-UA-Compatible"content="IE=edge">

<title>Storeis!</title>

<metaname="description"content="">

<metaname="viewport"content="width=device-width, initial-scale=1">

<linkrel="stylesheet"type="text/css"href="./vendor/zuck.js-master/zuck.css">

<linkrel="stylesheet"type="text/css"href="./vendor/zuck.js-master/skins/snapgram.css">

</head>

<body>

<!-- Make sure to have a div to put stories in. -->

<divid="stories"></div>

<!-- Import the Zuck library before you import your main code -->

<scriptsrc="./vendor/zuck.js-master/zuck.js"></script>

<scriptsrc="./js/main.js"></script>

</body>

</html>

Make sure that you download the Zuck js library and placed it somewhere in your project structure so that you can link to it. In this HTML file, I linked to the Zucks CSS stylesheet and the Zuck skin that I want to use. Inside the body, the only thing that I need is a div. This div will be passed to Zuck so that it can render the stories to it. Finally, I imported the Zuck javascript library then my main javascript code.

The Javascript

Now, in my main js file, I start by defining a function that will return an object with the properties specified:

function buildItem(id, type, length, src, preview, link, seen, time) {

// Using object short-hand (id: id)

return {

id,

type,

length,

src,

preview,

link,

seen,

time,

};

}

In that function, I pass in a whole bunch of parameters. It then outputs an object with those parameters. Keep in mind, I am using the property shorthand. So instead of saying {id: id} it’s just {id}.

Next, initialize the Zuck library with the below code:

const stories = new Zuck('stories', {

backNative:true,

autoFullScreen:'false',

skin:'Snapgram',

avatars:'true',

list:false,

cubeEffect:'true',

localStorage:true,

stories: [],

});

When initializing the Zuck object, make sure that the ‘stories’ parameter matches the div id that you defined in your HTML. Afterwards, I just loaded the Zuck library with the skin I wanted and also passed in a couple of options. You can find all the available options outlined in the Zuck readme.

Finally, let’s add a story to the story array. Inside of the array, add the following object:

stories: [

{

id:'vision',

photo:'https://cnet4.cbsistatic.com/img/QJcTT2ab-sYWwOGrxJc0MXSt3UI=/2011/10/27/a66dfbb7-fdc7-11e2-8c7c-d4ae52e62bcc/android-wallpaper5_2560x1600_1.jpg',

name:'Tech',

link:'',

lastUpdated:1492665454,

items: [

buildItem('1', 'photo', 3, 'https://pbs.twimg.com/profile_images/782474226020200448/zDo-gAo0_400x400.jpg', '', false, 1492665454),

buildItem('2', 'photo', 3, 'https://vignette4.wikia.nocookie.net/ironman/images/5/59/Robert-Downey-Jr-Tony-Stark-Iron-Man-3-Marvel-Disney.jpg/revision/latest?cb=20130611164804', '', '', false, 1492665454),

buildItem('3', 'video', 0, 'https://scontent-gru2-2.cdninstagram.com/t50.2886-16/14965218_193969377722724_482497862983221248_n.mp4', 'https://scontent-gru2-2.cdninstagram.com/t51.2885-15/e15/10597412_455246124639813_1360162248_n.jpg', '', false, 1492665454),

],

}],

This essentially will build a single story that contains three items. The items can be a mixture of photos or videos. You can also adjust the length of each story item. Zuck js works just like Snapchat and Instagram. So if the user has already seen the story (hence the seen boolean) it will automatically proceed to the next item.

So now you have everything you need for your basic Snapchat app. If you open up your Index.html file in the browser, you should see the single story listed. Clicking on it will open up the story in a viewer. From this point, you could make it more dynamic by pulling in stories from a REST endpoint or the user’s gallery if you wanted. To get this on Android or iOS simply wrap the app in a webview.

You can download the full code on Github