Created
October 11, 2019 06:49
-
-
Save notiv-nt/047bd8829c66749f245bfc707e4274fb to your computer and use it in GitHub Desktop.
feathersjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yarn add @feathersjs/express @feathersjs/feathers @feathersjs/socketio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="https://unpkg.com/@feathersjs/client@^4.3.0/dist/feathers.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script> | |
<script> | |
const socket = io(); | |
const app = feathers(); | |
app.configure(feathers.socketio(socket)); | |
app.service('posts') | |
.on('created', (post) => { console.dir(post) }); | |
app.service('posts') | |
.create({ date: new Date() }); | |
(async () => { | |
const posts = await app.service('posts') | |
.find({ | |
query: { $limit: 1 } | |
}); | |
console.dir(posts); | |
})(); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const feathers = require('@feathersjs/feathers'); | |
const express = require('@feathersjs/express'); | |
const socketio = require('@feathersjs/socketio'); | |
const app = express(feathers()); | |
class PostsService { | |
constructor() { | |
this.items = []; | |
} | |
async find({ query }) { | |
return this.items; | |
} | |
async create(data) { | |
this.items.push(data); | |
return data; | |
} | |
} | |
app.use(express.static(__dirname)); | |
app.use(express.json()); | |
app.configure(socketio()); | |
app.configure(express.rest()); | |
app.use('/posts', new PostsService()); | |
app.on('connection', conn => app.channel('stream').join(conn)); | |
app.publish(data => app.channel('stream')); | |
app | |
.listen(3000) | |
.on('listening', () => console.log(`Realtime server running on port 3000`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment