Slide 1

Slide 1 text

Building Real­time Vue App 2018/05/23 Vue.js Tokyo v­meetup #7 LT @joe_re

Slide 2

Slide 2 text

Who am I? twitter: @joe_re github: @joe­re ClassDo Inc CTO community: Nishinippori.rb, GraphQL Tokyo Organizer

Slide 3

Slide 3 text

What do you select for real­time communications? Polling Comet(Long polling) Server Sent Event Websocket

Slide 4

Slide 4 text

Socket.IO Socket.IO enables real­time bidirectional event­based communication. It works on every platform, browser or device, focusing equally on reliability and speed.

Slide 5

Slide 5 text

DEMO

Slide 6

Slide 6 text

Rooms and Namespaces

Slide 7

Slide 7 text

Rooms/Namespaces Good Points Enabling specific clients to send events is very easy. (All room numbers or only specific members or ...) If one of features causes a fatal error, it doesn't affect other features. Codebase become structured and easy to read

Slide 8

Slide 8 text

Where do we manage Socket communications on Vue Apps?

Slide 9

Slide 9 text

Socket.IO with Vuex

Slide 10

Slide 10 text

vuex­socketio­plugin https://github.com/joe­re/vuex­socketio­plugin Inspired by Vue­Socket.io https://github.com/MetinSeylan/Vue­Socket.io Integrate Socket.IO events to vuex actions and mutations Support multi connections Map Socket.IO namespaces to Vuex module namespaces

Slide 11

Slide 11 text

Map Socket.IO namespaces to Vuex module namespaces

Slide 12

Slide 12 text

Map Socket.IO namespaces to Vuex module namespaces

Slide 13

Slide 13 text

Map Socket.IO namespaces to Vuex module namespaces

Slide 14

Slide 14 text

Socket.IO Events Mapping

Slide 15

Slide 15 text

Socket.IO Events Mapping

Slide 16

Slide 16 text

Socket.IO Events Mapping

Slide 17

Slide 17 text

We can completely handle all websocket communications via the Vuex Store

Slide 18

Slide 18 text

but it may lose readability with product growth.. const sketchpad = { namespaced: true, state: { connect: false, ... }, mutations: { SOCKET_CONNECT (state) { ... }, SOCKET_RECEIVE_SHAPE (state, payload) { ... }, ADD_SHAPE (state, payload) { ... }, DELETE_SHAPE (state, payload) { ... }, OTHER_MUTATION(state, payload) { ... }, ... }, actions: { socket_RECEIVE_SHAPE (state, payload) { ... }, socket_CREATE_SHAPE (state, payload) { ... }, socket_ARCHIVE_SHAPE (state, payload) { ... }, fetchSketch (state, payload) { ... }, fetchAllList (state, payload) { ... }, ... } } combining events triggered by socket.IO and view. it makes very complex and difficult to read.

Slide 19

Slide 19 text

We can set any number of nested modules const store = new Vuex.Store({ modules: { account: { namespaced: true, state: { ... }, actions: { ... }, mutations: { ... }, // nested modules modules: { myPage: { state: { ... }, actions: { ... }, mutations: { ... }, }, // nested modules posts: { namespaced: true, state: { ... }, actions: { ... }, mutations: { ... }, } } } } })

Slide 20

Slide 20 text

adding a prefix of action and mutation as child module name. const store = new Vuex.Store({ modules: { videochat, sketchpads, ... } plugins: [ createSocketioPlugin([ ... ]), { actionPrefix: 'socket/', mutationPrefix: 'socket/' }), ] })

Slide 21

Slide 21 text

separate Socket.IO Events and other ones. const socket = { namespaced: true, state: { ... }, mutations: { CONNECT (state) { ... }, RECEIVE_SHAPE (state, payload) { ... }, }, actions: { RECEIVE_SHAPE (state, payload) { ... }, CREATE_SHAPE (state, payload) { ... }, ARCHIVE_SHAPE (state, payload) { ... }, } } const sketchpad = { namespaced: true, modules: socket, state: { ... }, mutations: { ADD_SHAPE (state, payload) { ... }, DELETE_SHAPE (state, payload) { ... }, OTHER_MUTATION(state, payload) { ... } }, actions: { fetchSketch (state, payload) { ... }, fetchAllList (state, payload) { ... } } }

Slide 22

Slide 22 text

It's very readable but it may feel a bit strange.. To update view we modify the state in the 'parent'. Child(socket) modules are mostly state­less. Thus children needs to modify the parent's state. It looks like anti­pattern. (but currently we have no problem, it's very limited situation...)

Slide 23

Slide 23 text

How do you deal with real­time communications? Let's talk after this.

Slide 24

Slide 24 text

Thank you for your attention!