Download - socket.io on SmartFx
![Page 1: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/1.jpg)
Socket.IO on
SmartFX
Takeshi Morita @takeshyMinkabu Inc.
![Page 2: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/3.jpg)
What is SmartFX?
![Page 4: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/5.jpg)
Coding Hintsfor the usage of Socket.IO
![Page 6: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/13.jpg)
Operation of Socket.IO server
![Page 14: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/14.jpg)
System Configulation
![Page 15: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/16.jpg)
Redundancy and Load Balancing Sequences
![Page 17: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/19.jpg)
Socket.IO ShutdownSequences
![Page 20: socket.io on SmartFx](https://reader034.vdocument.in/reader034/viewer/2022051512/53fe010b8d7f72a81c8b4c58/html5/thumbnails/20.jpg)
Thank you!