2

Brief

I am trying to connect Node.js[client] and Python [server]. Python server only supports WebSocket where Node.js socket.io is trying with long polling setting transport: [websocket].after all setup I am getting no data and node.js socket.io is trying to re-connect to server all the time.

Requirement

Python and Node.js connected with each other by websocket.where Node.js is a client and Python is server side.

Code

  1. Python

    # IMPORT'S
    import asyncio # core: python async-await library.
    import string # core: python string manipulatation library.
    import json # core: json library.
    import pydash # pip: utility module.
    from starlette.applications import Starlette # pip: http framework.
    from starlette.responses import JSONResponse # pip: http response handler.
    from starlette.websockets import WebSocket # pip: websocket library.
    import uvicorn # pip: server runner.
    
    # SYSTEM IMPORT'S
    from system import Connect # system: load engine connection.
    from www.template import Template # system: route template loader
    
    # ENGINE: STARLETTE
    App = Starlette()
    App.debug = True
    
    # GLOBAL'S
    _port = 8000
    _host = '0.0.0.0'
    
    # HANDLER: APP
    """
    Details: Allow's external connection to interact
    with system core.rightnow socket connection can
    be made.
    """
    class App:
       def __init__(self, scope):
           assert scope['type'] == 'websocket'
           self.scope = scope
    
       async def __call__(self, receive, send):
          # load and accept socket.io connection.
          _WebSocket = WebSocket(self.scope, receive=receive, send=send)
          await _WebSocket.accept()
    
          # local variable's.
          _templateName = _WebSocket.query_params['name']
    
          # only proceed if templateName is defined.
          if _templateName:
             # send json response to client.
             await _WebSocket.send_json({ 'templateReply': 'RecivedData' }, mode= 'binary')
    
        # close socket connection.
        await _WebSocket.close()
    
      # return none. as application is
      # run on socket.
      return None
    
    # SERVER RUN
    if __name__ == '__main__':
      # run server.
      uvicorn.run(App, host= _host, port = _port)
    
  2. Node.js

    // if _user information found than do login verification. else do
    // refresh @Cr for user information.
    // if _BroadCast contains data set.
    let _io = IO.connect('http://localhost:8000', {
      'path': '/chat',
      'transports': ['websocket'],
      'query': {
        'name': 'monk',
        'POST': 'dotsinspace@gmail.com'
      }
    })
    
    _io.on('connection', async () => {
       console.log('connected to user....') // log: nothing
    })
    _io.on('reconnecting', (__connection) => {
       console.log(__connection) // log: 2, 3 etc....
    })
    
  3. Result Socket Response

Manu Yadav
  • 115
  • 1
  • 9
  • Node.js usually used as a server, not a client. Are you using this? https://github.com/socketio/socket.io-client – yeya Feb 27 '19 at 14:36
  • Possible duplicate of [Node.js client for a socket.io server](https://stackoverflow.com/questions/10703513/node-js-client-for-a-socket-io-server) – yeya Feb 27 '19 at 14:38
  • Why not a client-side ? and yes i am using same link u provided :0 – Manu Yadav Feb 27 '19 at 14:38
  • Anyway - check this sample answer, it can help you to write the client https://stackoverflow.com/a/35427353/3107689 – yeya Feb 27 '19 at 14:38
  • Why not a client-side ? :( – Manu Yadav Feb 27 '19 at 14:40
  • No reason at all, I just say 'usually'. Anyway you need a way to test the server and the client with a tool you know already work. It will be much easier to debug. – yeya Feb 27 '19 at 15:48

0 Answers0