Skip to content

Commit

Permalink
feat: rewrite in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio committed Sep 19, 2023
1 parent 2bcb3ef commit 3553ef6
Show file tree
Hide file tree
Showing 23 changed files with 4,684 additions and 276 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ package-lock.json
.idea

# temporary/work/output folders
# build/
build/
out/
temp/
tmp/
dist/
3 changes: 0 additions & 3 deletions .taprc

This file was deleted.

3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

MIT License

Copyright (c) 2022 Alessandro Magionami
Copyright (c) 2023 Alessandro Magionami

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
# fastify-socket.io
# @ducktors/fastify-socket.io

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![CI workflow](https://github.com/alemagio/fastify-socket.io/workflows/CI%20workflow/badge.svg)
![CI workflow](https://github.com/ducktors/fastify-socket.io/workflows/CI%20workflow/badge.svg)

`fastify-socket.io` enables the use of [Socket.io](https://socket.io/) in a Fastify application.

Supports Fastify versions `3.x`
Supports Fastify versions `4.x`

## Install

```
npm i fastify-socket.io socket.io
```

## Usage

Require `fastify-socket.io` and register it as any other plugin, it will add a `io` decorator.

```js
const fastify = require('fastify')()
const fastify = require("fastify")();

fastify.register(require('fastify-socket.io'), {
fastify.register(require("fastify-socket.io"), {
// put your options here
})
});

fastify.get('/', (req, reply) => {
fastify.io.emit('hello')
})
fastify.get("/", (req, reply) => {
fastify.io.emit("hello");
});

fastify.listen({ port: 3000 })
fastify.listen({ port: 3000 });
```
For more details see [examples](https://github.com/alemagio/fastify-socket.io/tree/master/examples)

For more details see [examples](https://github.com/ducktors/fastify-socket.io/tree/master/examples)

You can use it as is without passing any option, or you can configure it as explained by Socket.io [doc](https://socket.io/docs/server-api/).

Expand All @@ -36,13 +40,12 @@ The plugin also adds an `onClose` hook which closes the socket server when the `

## Typescript

From v4 types will no longer be included in the plugin package but will need to be installed separately:
```
npm i -D types-fastify-socket.io
```
The `io` decorator is NOT typed.

This is necessary to allow, eventually, the developer to be able to define custom types and make use of the `socket.io` new types system ([doc](https://socket.io/docs/v4/typescript/)).

For more info see the [example](https://github.com/ducktors/fastify-socket.io/tree/master/examples)

## Acknowledgements

The code is a port for Fastify of [`socket.io`](https://github.com/socketio/socket.io).
Expand Down
34 changes: 18 additions & 16 deletions examples/basic/server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
const fastify = require('fastify')
const socketio = require('../..')
const { join } = require('path')
const { readFile } = require('fs').promises
const fastify = require("fastify");
const socketio = require("../..");
const { join } = require("path");
const { readFile } = require("fs").promises;

const app = fastify({ logger: true })
const app = fastify({ logger: true });

app.register(socketio)
app.register(socketio);

app.get('/', async (req, reply) => {
const data = await readFile(join(__dirname, '..', 'index.html'))
reply.header('content-type', 'text/html; charset=utf-8')
reply.send(data)
})
app.get("/", async (req, reply) => {
const data = await readFile(join(__dirname, "..", "index.html"));
reply.header("content-type", "text/html; charset=utf-8");
reply.send(data);
});

app.ready(err => {
if (err) throw err
app.ready((err) => {
if (err) throw err;

app.io.on('connect', (socket) => console.info('Socket connected!', socket.id))
})
app.io.on("connect", (socket) =>
console.info("Socket connected!", socket.id)
);
});

app.listen({ port: 3000 })
app.listen({ port: 3000 });
22 changes: 0 additions & 22 deletions examples/typescript-example/package.json

This file was deleted.

41 changes: 25 additions & 16 deletions examples/typescript-example/server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import fastify from 'fastify'
import socketioServer from 'fastify-socket.io'
import { join } from 'path'
const { readFile } = require('fs').promises
import fastify from "fastify";
import socketioServer from "../../src";
import { join } from "node:path";
import { readFile } from "node:fs/promises";
import { Server } from "socket.io";

const app = fastify({ logger: true })
const app = fastify({ logger: true });

app.register(socketioServer)
app.register(socketioServer);

app.get('/', async (req, reply) => {
const data = await readFile(join(__dirname, '..', 'index.html'))
reply.header('content-type', 'text/html; charset=utf-8')
reply.send(data)
})
app.get("/", async (_req, reply) => {
const data = await readFile(join(__dirname, "..", "index.html"));
reply.header("content-type", "text/html; charset=utf-8");
reply.send(data);
});

app.ready(err => {
if (err) throw err
app.ready((err) => {
if (err) throw err;

app.io.on('connection', (socket: any) => console.info('Socket connected!', socket.id))
})
app.io.on("connection", (socket: any) =>
console.info("Socket connected!", socket.id)
);
});

app.listen({ port: 3000 })
app.listen({ port: 3000 });

declare module "fastify" {
interface FastifyInstance {
io: Server<{ hello: string }>;
}
}
10 changes: 0 additions & 10 deletions examples/typescript-example/tsconfig.json

This file was deleted.

11 changes: 0 additions & 11 deletions index.js

This file was deleted.

65 changes: 32 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
{
"name": "fastify-socket.io",
"version": "4.0.0",
"description": "Fastify plugin for socket.io",
"main": "index.js",
"types": "./types/index.d.ts",
"name": "@ducktors/fastify-socket.io",
"version": "0.0.2",
"private": false,
"main": "dist/index.js",
"scripts": {
"lint": "standard",
"test": "npm run lint && npm run unit && cd types && npm run test",
"typescript": "tsd",
"unit": "tap --100 test/*.test.js"
"build": "tsc --noEmit && tsup-node",
"format": "rome format --write .",
"lint": "rome check .",
"preinstall": "npx only-allow pnpm",
"prepare": "husky install",
"prepublish": "pnpm build",
"release": "pnpm build && changeset publish",
"test": "vitest run",
"test:ci": "vitest run --reporter=junit --reporter=default --coverage",
"test:watch": "vitest --watch"
},
"author": "Alessandro Magionami - @alemagio",
"repository": {
"type": "git",
"url": "git+https://github.com/alemagio/fastify-socket.io.git"
},
"license": "MIT",
"dependencies": {
"fastify-plugin": "^3.0.0"
"fastify-plugin": "^4.5.1",
"tslib": "^2.6.1"
},
"devDependencies": {
"@types/node": "^16.11.10",
"fastify": "^4.0.1",
"fastify-tsconfig": "^1.0.1",
"socket.io": "^4.5.1",
"standard": "^16.0.4",
"tap": "^15.1.5",
"tsd": "^0.19.0",
"typescript": "^4.5.2"
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"@ducktors/tsconfig": "^0.1.1",
"@types/node": "^16.18.39",
"@vitest/coverage-istanbul": "^0.28.5",
"fastify": "^4.21.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.3",
"rome": "11.0.0",
"socket.io": "^4.7.1",
"tsup": "^6.7.0",
"typescript": "^4.9.5",
"vitest": "^0.28.5"
},
"keywords": [
"fastify",
"socket.io"
"files": [
"dist"
],
"ts-standard": {
"ignore": [
"examples"
]
},
"peerDependencies": {
"socket.io": "^4.4.0"
"fastify": "4.x.x"
}
}
Loading

0 comments on commit 3553ef6

Please sign in to comment.