Upload
-
View
1.560
Download
0
Embed Size (px)
DESCRIPTION
a case study of forex information website using socket.io
Citation preview
Socket.IO on
SmartFX
Takeshi Morita @takeshyMinkabu Inc.
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
What is 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
Coding Hintsfor the usage of Socket.IO
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
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
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
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.
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
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
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
Operation of Socket.IO server
System Configulation
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
Redundancy and Load Balancing Sequences
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
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
Socket.IO ShutdownSequences
Thank you!