-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-2-real-time-api.bsh
104 lines (79 loc) · 3.38 KB
/
example-2-real-time-api.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#
## example-2-real-time-api.bsh 2018-08-30
#
# REMEMBER: in-memory-db is just that, in-memory - not shared beyond either the
# browser or the server. in other words, all data is "local".
#
## adapted entirey from: https://docs.feathersjs.com/guides/basics/real-time.html
sudo curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash - ;
sudo yum --assumeyes install nodejs ;
npm --version ;
node --version ;
npm install @feathersjs/feathers --save ;
npm install @feathersjs/express --save ;
npm install feathers-memory --save ;
npm install @feathersjs/socketio @feathersjs/socketio-client socket.io-client --save ;
cat > ./app.js <<END_OF_APP_JS;
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
const memory = require('feathers-memory');
// This creates an app that is both, an Express and Feathers app
const app = express(feathers());
// Turn on JSON body parsing for REST services
app.use(express.json())
// Turn on URL-encoded body parsing for REST services
app.use(express.urlencoded({ extended: true }));
// Set up REST transport using Express
app.configure(express.rest());
// Configure the Socket.io transport
app.configure(socketio());
// On any real-time connection, add it to the `everybody` channel
app.on('connection', connection => app.channel('everybody').join(connection));
// Publish all events to the `everybody` channel
app.publish(() => app.channel('everybody'));
app.use(express.static('public')); // added by mark
// Initialize the messages service
app.use('messages', memory());
// Set up an error handler that gives us nicer errors
app.use(express.errorHandler());
// Start the server on port 3030
const server = app.listen(3030);
server.on('listening', () => console.log('Feathers API started at :3030'));
app.service('messages').on('created', data => console.log('created on server', JSON.stringify(data))); // added by mark (per daffl)
END_OF_APP_JS
mkdir ./public/ ;
cat > ./public/index.html <<END_OF_INDEX_HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Feathers Basics</title>
</head>
<body>
<h1>Welcome to Feathers</h1>
<p>Open up the console in your browser.</p>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script type="text/javascript" src="//unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js"></script>
<script type="text/javascript" src="//unpkg.com/feathers-memory@^2.0.0/dist/feathers-memory.js"></script>
<script>
// Create a websocket connecting to our Feathers server
const socket = io(':3030');
// Listen to new messages being created
socket.on('messages created', message =>
console.log('Someone created a message', message)
);
socket.emit('create', 'messages', {
text: 'Hello from socket'
}, (error, result) => {
if (error) throw error
socket.emit('find', 'messages', (error, messageList) => {
if (error) throw error
console.log('Current messages', messageList);
});
});
</script>
</body>
</html>
END_OF_INDEX_HTML
node app.js ;