socket.io on smartfx

20
Socket.IO on SmartFX Takeshi Morita @takeshy Minkabu Inc.

Upload: -

Post on 27-Aug-2014

1.560 views

Category:

Software


0 download

DESCRIPTION

a case study of forex information website using socket.io

TRANSCRIPT

Page 1: socket.io on SmartFx

Socket.IO on

SmartFX

Takeshi Morita @takeshyMinkabu Inc.

Page 2: socket.io on SmartFx

CONTENTS What is SmartFX? Coding Hints for the usage of Socket.IO

Considerations on using Socket.IO The Middleware, Socket.IO-ReqEv

Operation of Socket.IO server Redundancy and load balancing Shutdown in case of deploy

Page 3: socket.io on SmartFx

What is SmartFX?

Page 4: socket.io on SmartFx

What is SmartFX?

A free site for smartphone developed by Minkabu to provide FX information including news, charts and predictions

A real-time Single Page Application based on Socket.IO and Backbone.js

Go to - http://smartfx.jp or real URL http://smartfx.minkabu.jp

Page 5: socket.io on SmartFx

Coding Hintsfor the usage of Socket.IO

Page 6: socket.io on SmartFx

Considerationson using Sockeet.IO

Socket.IO has powerful Socket managing tools of “NameSpace”, “Rooms” and “Events”

Their interchangeable feature, there's more than one way to manage a broadcast client.

We, therefore, introduced our own coding rules in SmartFX

Page 7: socket.io on SmartFx

Coding Rules in SmartFX

One Object for One NameSpace One Event for One Room with One

EventName Each event shall be emitted using Room feature

Define 3 Types of Event of “message”, ”reply” and ”error” “message” shall be used for the data transfer

from a client to the server “reply” and ”error” shall be used for from the

server to a client

Page 8: socket.io on SmartFx

The Middleware, Stock.IO-ReqEv

With coding rules, succeeded to create a Middleware with specialized feature of Stock.IO procedures

We named the Middleware “Socket.IO-ReqEv” You can install below

npm install socket.io-reqev

Page 9: socket.io on SmartFx

How to use Socket.IO-ReqEv-the server side-

Only 2 steps1. Create a IOReqEv object with an initial value

of a Socket.IO object.2. Call “register” method giving NameSpace &

object met with the rule of IO-ReqEv

*Example above shows the simplest case of one set of registration. This “register” method can be called repeatedly.

Page 10: socket.io on SmartFx

Rules to pass objects to Socke.IO-ReqEv

1. Set event name to the property of array “events”2. Emit event with data, inheriting EventEmitter3. Implement a method “request”, which Socket.IO-ReqEv calls when a message from client includes a parameter of “requests” . This works similar to HTTP GET

*Codes on the left runs to broadcast events every second/minute and emit the current time

Page 11: socket.io on SmartFx

How to use Socket.IO-ReqEv-the client side-

Only 2 steps on browser1. Create IOReqEvClient, giving the url which includes the address of Socket.IO and namespace, and callback2. Call a method “watch” to subscribe an event and to get a request

*unsubscribe automatically unnecessary events previously called, once the method “watch” is called again with another events

Page 12: socket.io on SmartFx

Implementation of Socket.IO-ReqEv

The library of Socket.IO-ReqEv is written with just less than 60 coding lines

Moreover, the library is written with public properties only on Socket.IO 1.0, while there were some non-public properties on 0.9.x

The source code of Socket.IO-ReqEv is available at url belowhttps://github.com/takeshy/socket.io-reqev

Page 13: socket.io on SmartFx

Operation of Socket.IO server

Page 14: socket.io on SmartFx

System Configulation

Page 15: socket.io on SmartFx

Redundancy and Load Balancing

Redundancy and load balancing are accomplished- Socket.IO server updates its record on the DB

periodically WebServer gets a list of address of Socket.IO

server recently updated from the DB WebServer Returns one of them randomly to a

client

Page 16: socket.io on SmartFx

Redundancy and Load Balancing Sequences

Page 17: socket.io on SmartFx

Shutdown in case of Deploy

Delete command in demon management tool of pm2 does not work under the situation of Socket.IO being always connected to client

To solve this problem, developed a self-shutdown mechanism of Socket.IO server

Page 18: socket.io on SmartFx

The Workflow of Deploy Update app source code in the Socket.IO server, and run the script

which executes pm2 command of “delete” and “start”. (by capistrano)

Set “inactive” to the status field of the Socket.IO record on the DB

Once the Socket.IO detects its own record set as “inactive”, the server broadcasts inactive event to clients and set timer to kill itself a minute later

A client which received inactive event displays a popup message saying “disconnected the server” in case the user stays more than 1 minute. (in case a user goes to different page, the client gets another server address, then network connection stays)

Set “activate” to the status field of the Socket.IO record on the DB

Page 19: socket.io on SmartFx

Socket.IO ShutdownSequences

Page 20: socket.io on SmartFx

Thank you!