Giter Site home page Giter Site logo

Comments (7)

mcollina avatar mcollina commented on July 24, 2024 2

I'm sorry that you need something. However, this is open source and I have no bandwidth to help right now.

from fastify-websocket.

mcollina avatar mcollina commented on July 24, 2024

Does this not work? https://github.com/fastify/fastify-websocket/blob/master/test/router.js#L10-L46.

from fastify-websocket.

eliecer2000 avatar eliecer2000 commented on July 24, 2024

What library or framework do you use in the frontend?

I can give you the example of vue-native-socket

import VueNativeSock from 'vue-native-websocket';

      Vue.use(VueNativeSock, 'ws://localhost:9000/', {
        protocol: tokenData, // optional
        reconnection: true, // (Boolean) whether to reconnect automatically (false)
        reconnectionAttempts: 10, // (Number) number of reconnection attempts before giving up (Infinity),
        reconnectionDelay: 6000 // (Number) how long to initially wait before attempting a new (1000)
      })

and server

fastify.register(require('fastify-websocket'), {
  handle(conn) {
    conn.pipe(conn) // creates an echo server
  },
  options: {
    maxPayload: 1048576,
    verifyClient(info, next) {
      try {
        const token = info.req.headers['sec-websocket-protocol']
        return next(true)
      } catch (err) {
        return next(false)
      }
    }
  }
})


  fastify.route({
    method: 'GET',
    url: '/',
    handler: (req, reply) => {
      reply.send({ route: 'OK' })
    },
    wsHandler: (conn, req, params) => {
      conn.setEncoding('utf8')
      conn.on('data', chunk => {
        console.log(chunk)
        conn.socket.send("Message OK")
      })
    }
  })

I hope it helps you.

from fastify-websocket.

satyamurthy515 avatar satyamurthy515 commented on July 24, 2024

from fastify-websocket.

satyamurthy515 avatar satyamurthy515 commented on July 24, 2024

from fastify-websocket.

mandaputtra avatar mandaputtra commented on July 24, 2024

@satyamurthy515

You can connect from websockets like these :

// Your front end code
// connect to server
const ws = new WebSocket()

// send to server
ws.send('message')

// receive messages
ws.on('message', (message) => {
  console.log(message)
})

No matter youre on React, Vue, Angular, or SomeWhatNewJSFramework, the API Of websocket is same take a look here

from fastify-websocket.

satyamurthy515 avatar satyamurthy515 commented on July 24, 2024

Thank you guys for your valuable comments. I got it.

from fastify-websocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.